Minitest Fixture getting error: Couldn't find User with 'id'=

Minitest Fixture getting error: Couldn't find User with 'id'=

我在一个文件中有十几个测试最近在安装过程中开始失败。该文件包括十几个其他通过的测试,但我看不出它们之间有任何区别。

这是我遇到的错误:

ERROR["test_teacher_edits_public_objective", ObjectivesFormTest, 38.557620885781944]
 test_teacher_remove_seminar_from_objective#ObjectivesFormTest (38.56s)
ActiveRecord::RecordNotFound:         ActiveRecord::RecordNotFound: Couldn't find User with 'id'=522600246
        test/test_helper.rb:45:in `setup_users'
        test/integration/objectives/objectives_form_test.rb:10:in `setup'

objectives_form_test.rb

def setup
    setup_users()
    setup_seminars
    setup_objectives()
    setup_labels()
    setup_questions()

    @old_objective_count = Objective.count
end

test_helper.rb

def setup_users
  @admin_user = users(:michael)
  @teacher_1 = users(:archer)
  @other_teacher = users(:zacky)
  @unverified_teacher = users(:user_1)
  @teacher_3 = @teacher_1.school.teachers[3]
  @student_1 = users(:student_1)
  @student_2 = users(:student_2)
  @student_3 = users(:student_3)
  @other_school_student = users(:other_school_student)
  @student_90 = users(:student_90)
end

如您所见,此设置方法包括十个为重要用户建立的实例变量。前五个按预期通过。但是 student_1 由于某些深不可测的原因而失败。

基于我在 Whosebug 上看到的类似问题,我尝试重置并重新播种我的数据库。我已经在两个开发环境中完成了这个,并使用 ENV="test"

当我在 setup_users 方法中调用调试器时,通过的测试显示存在 135 个用户,包括 student_1。失败的测试仅显示存在 35 个用户。

当我 运行 将它们隔离时,所有这些失败的测试都通过了。

提前感谢您的任何见解。

如果您使用 database_cleaner,在一些极端情况下,由于默认的 database_cleaner 配置,您的表可能会过早清理。这似乎更经常出现在与浏览器相关的测试中。

一些更详细地解释问题的参考资料以及解决方案(对于 RSpec,minitest 应该是类似的)

http://www.virtuouscode.com/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/

(参见下面的 RSpec 和最小测试示例)

https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example