my model has default_scope(:order => \'created_at\' ) my tests (rspec, factory girl, shoulda, etc.) are:
require \'spec/spec_helper.rb\'
describe CatMembers
Most likely, you'll have more than one model with a similar default scope (if not, mostly ignore this method) so you can put this Rspec example into a shared_example where you can call it from a variety of model specs.
My preferred method of checking a default scope is to make sure the default ActiveRecord::Relation
has the expected clause (order
or where
or whatever the case may be), like so:
spec/support/shared_examples/default_scope_examples.rb
shared_examples_for 'a default scope ordered by created_at' do
it 'adds a clause to order by created_at' do
described_class.scoped.order_clauses.should include("created_at")
end
end
And then in your CatMembership
spec (and any other spec that has the same default scope), all you need to is:
spec/models/cat_membership_spec.rb
describe CatMembership
it_behaves_like 'a default scope ordered by created_at'
# other spec examples #
end
Finally, you can see how this pattern can be extended to all sorts of default scopes and keeps things clean, organized and, best of all, DRY.