Ruby新手在这里试图掌握rspec,rails,黄瓜。
我不确定为什么会返回以下错误:
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)>'
来自以下型号规格文件:
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
我确定这很简单,但我无法理解它?
答案 0 :(得分:1)
在let
来电中,您需要实际“创建”用户。你刚刚建立它没有保存。所以当你打电话时
user = User.find_by_username('username')
由于记录未保存且无法找到,因此返回nil
。
另外,#find_by_username
是Rails的一部分,你真的不需要测试它。