【发布时间】:2026-01-15 23:35:02
【问题描述】:
我正在尝试使用 factory girl 创建两个用户,每个用户都有不同的sign_in_count(使用设计)。到目前为止,第一个测试很好,但是当第二个测试时,我收到第二个用户未初始化的错误。如果重要的话,测试套件是 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>'
【问题讨论】:
标签: ruby-on-rails devise capybara factory-bot testunit