Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

The behavior is as intended, the only problem is that on your test database, there is no record of the SoftwareVersion. You only need to populate the database before running the tests. As you can see, the query is done perfectely, WHERE software_versions.is_testing = 0, with zero means false as in your default_scope.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top