Ruby newbie here trying to get to grips with rspec, rails, cucumber.
I’m not sure why this is returning the following error:
1) User can be found by username
Failure/Error: user.should_not be_valid
NoMethodError:
undefined method `valid?' for nil:NilClass
# ./spec/models/user_spec.rb:26:in `block (2 levels) in <top (required)>'
From the following model spec file:
require 'spec_helper'
describe User do
let(:user) { User.new(
:username => 'username',
:password => 'password'
)
}
it "should be createable" do
user.should be_valid
end
it "cannot have an empty username" do
user.username = nil
user.should_not be_valid
end
it "cannot have an empty password" do
user.password = nil
user.should_not be_valid
end
it "can be found by username" do
user = User.find_by_username('username')
user.should be_valid
end
it "can be deleted"
end
I’m sure it’s something simple but I can’t figure it out?
In your
letcall you need to actually “create” the user. You just built it without saving. So when you callThat is returning
nilas the record is not saved and can’t be found.Also,
#find_by_usernameis part of Rails, you don’t really need to be testing that.