尝试在 factory_girl 中创建多个用户时获取未初始化的常量
Getting unitialized constant when trying to create more than one user in factory_girl
我正在尝试创建两个工厂女工用户,每个用户都有不同的 sign_in_count
(使用 Devise)。到目前为止,第一个测试进行得很好,但是当进行第二个测试时,我收到一个错误,指出第二个用户未初始化。如果重要的话,测试套件是 Test::Unit(不是 RSpec)。
这是测试
require 'test_helper'
class ApplicationControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
test 'user should be redirected to profile edit on first login' do
@user = create(:user, sign_in_count: 0)
visit(new_user_session_path)
fill_in('user_email', :with => @user.email)
fill_in('user_password', :with => 'foobar')
click_button('Log in')
assert_current_path(edit_user_registration_path)
logout(@user)
end
test 'user should be taken to root on subsequent logins' do
@other_user = create(:other_user, sign_in_count: 5)
visit(new_user_session_path)
fill_in('user_email', :with => @other_user.email)
fill_in('user_password', :with => 'foobar')
click_button('Log in')
assert_current_path(root_path)
end
end
和工厂
FactoryGirl.define do
sequence(:email) { |n| "person#{n}@example.com" }
sequence(:id) { |n| n }
factory :user do
email
password 'foobar'
password_confirmation 'foobar'
id
after(:create) { |user| user.confirm }
end
factory :other_user do
email
password 'foobar'
password_confirmation 'foobar'
id
after(:create) { |user| user.confirm }
end
end
和错误
ERROR["test_user_should_be_taken_to_root_on_subsequent_logins", ApplicationControllerTest, 0.8529229999985546]
test_user_should_be_taken_to_root_on_subsequent_logins#ApplicationControllerTest (0.85s)
NameError: NameError: uninitialized constant OtherUser
test/controllers/application_controller_test.rb:17:in `block in <class:ApplicationControllerTest>'
FactoryGirl 试图找到名称为 class OtherUser
的模特。但是你没有那个模型。相反,您想使用 User
模型,但您使用的是不同的工厂名称。
因此,添加 class 名称将解决问题
factory :other_user, class: User do
email
password 'foobar'
password_confirmation 'foobar'
id
after(:create) { |user| user.confirm }
end
我正在尝试创建两个工厂女工用户,每个用户都有不同的 sign_in_count
(使用 Devise)。到目前为止,第一个测试进行得很好,但是当进行第二个测试时,我收到一个错误,指出第二个用户未初始化。如果重要的话,测试套件是 Test::Unit(不是 RSpec)。
这是测试
require 'test_helper'
class ApplicationControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
test 'user should be redirected to profile edit on first login' do
@user = create(:user, sign_in_count: 0)
visit(new_user_session_path)
fill_in('user_email', :with => @user.email)
fill_in('user_password', :with => 'foobar')
click_button('Log in')
assert_current_path(edit_user_registration_path)
logout(@user)
end
test 'user should be taken to root on subsequent logins' do
@other_user = create(:other_user, sign_in_count: 5)
visit(new_user_session_path)
fill_in('user_email', :with => @other_user.email)
fill_in('user_password', :with => 'foobar')
click_button('Log in')
assert_current_path(root_path)
end
end
和工厂
FactoryGirl.define do
sequence(:email) { |n| "person#{n}@example.com" }
sequence(:id) { |n| n }
factory :user do
email
password 'foobar'
password_confirmation 'foobar'
id
after(:create) { |user| user.confirm }
end
factory :other_user do
email
password 'foobar'
password_confirmation 'foobar'
id
after(:create) { |user| user.confirm }
end
end
和错误
ERROR["test_user_should_be_taken_to_root_on_subsequent_logins", ApplicationControllerTest, 0.8529229999985546]
test_user_should_be_taken_to_root_on_subsequent_logins#ApplicationControllerTest (0.85s)
NameError: NameError: uninitialized constant OtherUser
test/controllers/application_controller_test.rb:17:in `block in <class:ApplicationControllerTest>'
FactoryGirl 试图找到名称为 class OtherUser
的模特。但是你没有那个模型。相反,您想使用 User
模型,但您使用的是不同的工厂名称。
因此,添加 class 名称将解决问题
factory :other_user, class: User do
email
password 'foobar'
password_confirmation 'foobar'
id
after(:create) { |user| user.confirm }
end