Samuel Mullen

What could possibly go wrong?

David Chelimsky's Answer to Testing Named Scopes

I was looking for the best way to test named scopes in Rails using Rspec and I ran across the following answer by David Chelimsky (Rspec’s creator) in the Rspec Google Group:

Testing Named Scopes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
describe User, ".admins" do
  it "includes users with admin flag" do
    admin = User.create! :admin => true
    User.admin.should include(admin)
  end

  it "excludes users without admin flag" do
    non_admin = User.create! :admin => false
    User.admin.should_not include(non_admin)
  end
end

class User < ActiveRecord::Base
  named_scope :admins, :conditions => {:admin => true}
end

Read the entire thread for context.