I've got a dozen tests in one file that recently began failing during setup. The file includes a dozen other tests that are passing, but I can't see any difference between them.
Here is the error that I'm getting:
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
As you can see, this setup method includes ten instance variables that are established for important users. The first five pass as expected. But student_1 fails for some unfathomable reason.
Based on similar questions that I've seen on StackOverflow, I've tried resetting and reseeding my databases. I've done this in both development environment, and using ENV="test"
When I put a call to debugger in the setup_users method, the passing tests show that 135 users exist, including student_1. The failing tests only show that 35 users exist.
All of these failing tests pass when I run them in isolation.
Thank you in advance for any insight.
If you're using database_cleaner there are a few edge cases where your tables may get cleaned too early because of the default database_cleaner configuration. This seems to pop up more often with browser-related tests.
A few references that explain the issue in more detail, along with solutions (for RSpec, minitest should be similar however)
(See the RSpec and minitest examples below)
https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example