Minitest Fixture收到错误:找不到具有' id' =的用户

时间:2018-05-18 02:29:25

标签: ruby-on-rails minitest fixtures

我在一个文件中进行了十几次测试,最近在安装过程中失败了。该文件包含其他十几个正在通过的测试,但我看不出它们之间存在任何差异。

以下是我收到的错误:

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因为一些不可思议的原因而失败了。

根据我在StackOverflow上看到的类似问题,我尝试重置和重新植入我的数据库。我已经在开发环境中完成了这项工作,并使用ENV =" test"

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

当我单独运行它们时,所有这些失败的测试都会通过。

提前感谢您的任何见解。

1 个答案:

答案 0 :(得分:1)

如果您正在使用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