我有一个简单的用户工厂,如下所示:
FactoryGirl.define do
factory :user do
name "jeff"
email "jeff@lint.com"
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
正在返回 nil。但我不明白为什么。当我在控制台上尝试这段代码时,它工作得很好。那我做错了什么?我对 Factory Girl 很陌生。
我也在寻找的是在不使用实例变量的情况下做到这一点。
最佳答案
试试这个
describe "return value of authenticate method", focus: true do
before do
@user = FactoryGirl.create(:user)
end
let(:found_user) { User.find_by_email(@user.email) }
it "can return value of authenticate method" do
expect(@user).to eq found_user.authenticate(@user.password)
end
end
关于ruby-on-rails-3 - 如何使用 rspec 和 factory girl 设置我的身份验证数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14417273/