我有一个简单的用户工厂,看起来像这样:如何使用rspec和工厂女孩设置我的认证数据?
FactoryGirl.define do
factory :user do
name "jeff"
email "[email protected]"
password "foobar"
password_confirmation "foobar"
end
end
,我试图测试内置authenticate
方法,像这样:
describe "return value of authenticate method", focus: true do
before do
create(:user)
end
let(:found_user) { User.find_by_email(:email) }
it "can return value of authenticate method" do
expect(:user).to eq found_user.authenticate(:password)
end
end
我得到的错误是
NoMethodError:
undefined method `authenticate' for nil:NilClass
这可能意味着found_user
返回零。但我不明白为什么。当我在控制台上试用此代码时,它工作得很好。那么我做错了什么?我对Factory Girl很新颖。
我也在寻找的是没有使用实例变量的权利。
这个工程,但我想知道为什么只使用(:用户)不起作用。 –
因为您已将:user factory的值分配给@user。这就像一堂课。你必须先实例化它。 –
那么用户工厂返回的是什么?它不是像@user这样的对象吗? –