With let_it_be, we define variables that are created for the whole context. In case we update them in an example, there might be a discrepancy between the object and its value in the database, since the database is cleaned between examples, but not the object itself. This is what happened here with the `article` object. In one of the tests, we update its organization, and in another, we reupdate it. The problem is that the `article` object still thinks it has an organization, but it has been cleaned in the database. So setting the same organization id to the object doesn't trigger the SQL query, and the article in the database still doesn't have an organization attached. One solution is to use `reload: true` as a parameter of `let_it_be`: it will reload the object from the database at the beginning of each test. To make sure we don't have other dormant traps like this one, what I propose is to either have `reload: true`, or to have the object readonly to avoid getting to a difference between the object and its database value.
12 lines
328 B
Ruby
12 lines
328 B
Ruby
TestProf::LetItBe.configure do |config|
|
|
config.register_modifier :readonly do |record, val|
|
|
next record unless record.is_a?(::ActiveRecord::Base)
|
|
|
|
next record unless val
|
|
|
|
record.tap(&:readonly!)
|
|
end
|
|
|
|
config.alias_to :let_it_be_readonly, readonly: true
|
|
config.alias_to :let_it_be_changeable, reload: true
|
|
end
|