I would rather have this tested using a query and checking the results, but if you really must do it, one possible solution would be something like this for Rails 3:
CatMembership.scoped.to_sql.should == CatMembership.order(:created_at).to_sql
And on Rails 2:
CatMembership.default_scoping.should == [{:create=>{}, :find=>{:order=>"created_at"}}]
But I would not say these solutions are ideal since they show a lot of knowledge of the implementation (and you can see the implementation varies with different Rails versions).
Creating sample data, running an usual all query and checking the result is correctly ordered might have been simpler, would be closer to real unit testing and would work even as you upgrade your rails version.
In this case it would possibly be:
before do
@memberships = []
@memberships << CatMembership.create!
@memberships << CatMembership.create!
@memberships << CatMembership.create!
[ 1.hour.ago, 5.minutes.ago, 1.minute.ago ].each_with_index do |time, index|
membership = @memberships[index]
membership.created_at = time
membership.save
end
end
it 'should be correctly ordered' do
@sorted_memberships = CatMembership.all
@memberships.first.should == @sorted_memberships.last
@memberships.second.should == @sorted_memberships.second
@memberships.third.should == @sorted_memberships.first
end
It's much more verbose, but it's going to work even as you move forward on rails.
And now I have just noticed who asked the question :D
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…