What\'s the best way to test scopes in Rails 3. In rails 2, I would do something like:
Rspec:
it \'should have a top_level scope\' do
Category.top
I agree with others here that testing the actual results you get back and ensuring they are what you expect is by far the best way to go, but a simple check to ensure that a scope is adding the correct clause can also be useful for faster tests that don't hit the database.
You can use the where_values_hash to test where conditions. Here's an example using Rspec:
it 'should have a top_level scope' do
Category.top_level.where_values_hash.should eq {"parent_id" => nil}
end
Although the documentation is very slim and sometimes non-existent, there are similar methods for other condition-types, such as:
Category.order(:id).order_values
# => [:id]
Category.select(:id).select_values
# => [:id]
Category.group(:id).group_values
# => [:id]
Category.having(:id).having_values
# => [:id]
etc.
For default scopes, you have to handle them a little differently. Check this answer out for a better explanation.