Samuel Mullen What could possibly go wrong?

David Chelimsky's Answer to Testing Named Scopes

by Samuel Mullen

Posted on Mar 18, 2011


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:

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.


About me


I live in the greater Kansas City area with my beautiful wife, our two great kids, and our dog. I've been programming using Open Source technologies since '97 and I'm currently an independent software developer specializing in Ruby on Rails and iOS. I am for hire.