【发布时间】:2018-05-18 02:29:25
【问题描述】:
我在一个文件中有十几个测试,最近在安装过程中开始失败。该文件包含十几个其他正在通过的测试,但我看不出它们之间有什么区别。
这是我得到的错误:
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 个用户。
当我单独运行它们时,所有这些失败的测试都通过了。
提前感谢您提供任何见解。
【问题讨论】:
-
您是否在运行测试之前、期间或之后使用任何类型的工具(如 database_cleaner)来重置数据库?另外,哪个版本的 Rails?
-
仅在我的涉及 poltergeist 的测试中。该文件确实包含一些使用 poltergeist 的测试。但我没有想到这一点,因为这些测试并没有失败。
-
在使用 database_cleaner 时有几个边缘情况,由于配置,事情被过早清理。据我所知,受影响的测试通常是使用实际浏览器的测试,并且具有特定的数据库清理器配置(与事务相关)。 Capybara 通常也参与其中(驱动浏览器)。 RSpec 示例的描述比我能提供的更好(不确定此逻辑是否可以应用于 minitest):github.com/DatabaseCleaner/…
-
我将所有包含 poltergeist(以及因此的数据库清理程序)的测试移到了不同的文件中。这似乎解决了这个问题。您想将您的建议作为答案发布,以便我将其标记为我的首选答案吗?
-
当然可以。我发现另一个参考资料更详细地解释了交易问题。我也会补充的
标签: ruby-on-rails minitest fixtures