Ruby on rails 为什么可以';即使使用FactoryGirl创建了一个记录,我也不能在测试数据库中找到它吗?
我正在编写一个控制器规范,首先创建一个由3个Ruby on rails 为什么可以';即使使用FactoryGirl创建了一个记录,我也不能在测试数据库中找到它吗?,ruby-on-rails,ruby-on-rails-4,rspec,rspec-rails,rspec3,Ruby On Rails,Ruby On Rails 4,Rspec,Rspec Rails,Rspec3,我正在编写一个控制器规范,首先创建一个由3个@用户组成的数组。看起来是这样的: describe 'POST #create' do context 'when invited user IS an existing user' do before :each do @users = [ attributes_for(:user), attributes_for(:user), a
@用户组成的数组。看起来是这样的:
describe 'POST #create' do
context 'when invited user IS an existing user' do
before :each do
@users = [
attributes_for(:user),
attributes_for(:user),
attributes_for(:user)
]
end
it 'correctly finds User record of invited user' do
login_user
post :create, { email: @users.first[:email] }
invited_user = User.find_by(email: controller.params[:email])
expect(invited_user).to be_a(User)
end
end
end
当我使用此测试运行rspec
时,它失败并给出以下错误:
1) Users::InvitationsController POST #create when invited user IS an existing user correctly finds User record of invited user
Failure/Error: expect(invited_user).to be_a(User)
expected nil to be a kind of User(id: integer, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, remember_created_at: datetime, sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at: datetime, current_sign_in_ip: string, last_sign_in_ip: string, created_at: datetime, updated_at: datetime, first_name: string, confirmation_token: string, confirmed_at: datetime, confirmation_sent_at: datetime, unconfirmed_email: string, invitation_relation: string, avatar: string, invitation_token: string, invitation_created_at: datetime, invitation_sent_at: datetime, invitation_accepted_at: datetime, invitation_limit: integer, invited_by_id: integer, invited_by_type: string, invitations_count: integer, bio: text, last_name: string, gender: integer)
这是:user
的工厂:
factory :user do
association :family_tree
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
email { Faker::Internet.email }
password "password123"
password_confirmation "password123"
bio { Faker::Lorem.paragraph }
invitation_relation { Faker::Lorem.word }
# required if the Devise Confirmable module is used
confirmed_at Time.now
gender 1
end
我相信现在发生的事情是,User.find\u by
ActiveRecord lookup正在返回nil
但如果我在上面创建了这3个用户记录,为什么会返回nil?我看不出您在哪里创建用户。。。您只需在before块中构建一个用户属性数组(它们只是散列-不是真正的用户!)。方法只返回一个可用于创建用户的哈希。您可以发布user
类吗?你知道你的职位是成功还是失败吗?您可能遇到了验证错误。您使用哪个用户登录?此外,正如@gotva所提到的,您不会使用attributes调用创建用户,唯一的创建内容是post。