Testing is an important part of software development. Having a solid test suite allows you develop more confidently – enable you to make large refactorings without fear of unintended consequences. I test almost all the code I write.
Recently, I found a bug that allowed users to see ‘inactive’ products; so I wrote a test to prevent that. I’ve seen lots of specs that test active products like so:
1 2 3 4 5 6 7 8 9
Test like this are wordy and can lead to a ton of factoried objects. There’s got to be a better way!!
1 2 3 4 5
Ahhh how refreshing… See that
have_scope method there? That matcher is accomplished by applying the scope to the set of products and asserting that it should be the same
ActiveRecord::Relation. In most cases:
ActiveRecord::Relation + scope == ActiveRecord::Relation + scope + scope
so you can assert that your products relation is unchanged by adding the desired scope. This isn’t always the case though. Some
wheres just keep getting tacked onto the end of the relation… so it falls back to an array comparison, and that is unawesome. Anywho, the final product is:
This has helped me write legible and concise specs, mileage my vary.