I have a fixture for the model "SoftwareVersion":
testing:
id: 4
version: "4.0"
file_name: MyString4
is_testing: true
And a default scope for the model:
default_scope where(is_testing: false)
If I do this in a test:
software_versions(:testing)
I get an error:
ActiveRecord::RecordNotFound: Couldn't find SoftwareVersion with id=4 [WHERE `software_versions`.`is_testing` = 0]
Is that intended behavior? And how do I override it?
Yes this is intended behavir, Default scope will affect all queries. Use unscoped to start a clean query again:
SoftwareVersion.unscoped.where(is_testing: true)
SoftwareVersion.unscoped.find(id: 4)
etc.
But that apart, if you're using the same table (with fixtures) for storing both Production and Testing records, then you're doing it wrong. Rails uses a totally different database for testing, so you don't need a column to distinguish. And if you put the fixtures under test/fixtures
, they will never get loaded in production at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With