Ruby on rails 在factory_中尝试创建多个用户时获取统一化常量

Ruby on rails 在factory_中尝试创建多个用户时获取统一化常量,ruby-on-rails,devise,capybara,factory-bot,testunit,Ruby On Rails,Devise,Capybara,Factory Bot,Testunit,我正在尝试创建两个使用factory girl的用户,其中每个用户都有一个不同的登录计数(使用Desive)。到目前为止,第一个测试进行得很顺利,但是当第二个测试到来时,我得到一个错误,第二个用户没有初始化。测试套件是test::Unit(不是RSpec),如果这很重要的话 这是测试 require 'test_helper' class ApplicationControllerTest < ActionDispatch::IntegrationTest include Devis

我正在尝试创建两个使用factory girl的用户,其中每个用户都有一个不同的
登录计数
(使用Desive)。到目前为止,第一个测试进行得很顺利,但是当第二个测试到来时,我得到一个错误,第二个用户没有初始化。测试套件是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
错误呢

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>'
错误[“应在后续登录时将测试用户带到根用户”,ApplicationControllerTest,0.852922999985546]
测试(用户)(应)在(后续)登录(ApplicationControllerTest)(0.85s)时(应)将(用户)带到(根用户
NameError:NameError:未初始化的常量OtherUser
测试/控制器/应用程序控制器测试.rb:17:in'block in'

FactoryGirl试图找到类名为
OtherUser
的模型。但是你没有那种型号。相反,您希望使用
用户
型号,但使用的是不同的工厂名称

因此,添加类名将解决此问题

factory :other_user, class: User do
  email
  password 'foobar'
  password_confirmation 'foobar'
  id
  after(:create) { |user| user.confirm }
end

FactoryGirl尝试查找类名为
OtherUser
的模型。但是你没有那种型号。相反,您希望使用
用户
型号,但使用的是不同的工厂名称

因此,添加类名将解决此问题

factory :other_user, class: User do
  email
  password 'foobar'
  password_confirmation 'foobar'
  id
  after(:create) { |user| user.confirm }
end

这很有效,谢谢!感谢您的解释您可能也不想在工厂中设置id,它将由数据库自动设置,在本例中根本不需要其他用户工厂——只需创建user@oneWorkingHeadphone对事实上,你不需要一个新的工厂that@ThomasWalpole啊,是的,你是对的,这是我试图一起破解解决方案时留下的代码。谢谢你的接球!这很有效,谢谢!感谢您的解释您可能也不想在工厂中设置id,它将由数据库自动设置,在本例中根本不需要其他用户工厂——只需创建user@oneWorkingHeadphone对事实上,你不需要一个新的工厂that@ThomasWalpole啊,是的,你是对的,这是我试图一起破解解决方案时留下的代码。谢谢你的接球!