Last updated

Testing Rails 3 scopes revisited

In my previous article I told you about how I like to tests my scope. There was a fair amount of criticism on that post and after considering it all (and hearing Corey Haines’ talk on Arrrrcamp last friday), I’m convinced it’s the wrong path.

In essence Rails allows you to create a scope to generate a custom database query for you. Now, if you only test your configuration of Rails (as I did in my previous post), you don’t know if that scope works or not. If a piece directly relies on your database, you must test it against your database.

Also, your configuration may be correct, but what happens when you upgrade to a newer version of Rails or PostgreSQL? Does that configuration still work as advertised?

So, this is the proper way of testing your scopes:

 1class Post < ActiveRecord::Base
 2  scope :latest, order("created_at DESC")
 3end
 4
 5describe Post do
 6  context 'scopes' do
 7    before(:all) do
 8      @first = FactoryGirl.create(:post, :created_at => 1.day.ago)
 9      @last  = FactoryGirl.create(:post, :created_at => 4.day.ago)
10    end
11
12    it "should return posts in the correct order" do
13      Post.latest.should == [@first, @last]
14    end
15  end
16end

A note on speed

So, you now have to utilize the whole Rails stack and hit your database to perform this test, which is slow. But then you don’t upgrade Rails or your database very often, so you don’t have run this spec with every TDD Baby Step you take. (More on that in a later post.)