Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test scopes?

tried to find but with no success. Just wondering how could I test scopes in Rails 3.

Could be using rspec, shoulda or just a test unit.

Thanks.

Actually, I trying this way, but it's not complete test since it's still need to put the order() method.

The Scope:

scope :recents_available, where(:available => true, :locked => false).order("created_at DESC")

describe Job, ":recents_available" do

it "should have the scope" do
  Job.should respond_to(:recents_available)
end

it "should include recents jobs that are available and unlocked" do
  @job = Factory(:job, :available => true, :locked => false  )      
  Job.recents_available.should include(@job)
end

end

like image 205
Kleber S. Avatar asked Jan 30 '11 01:01

Kleber S.


People also ask

What should test scope include?

The scope of a test defines what areas of a customer's product are supposed to get tested, what functionalities to focus on, what bug types the customer is interested in, and what areas or features should not be tested by any means.

What is meant by testing scope?

A test scope shows the software testing teams the exact paths they need to cover while performing their application testing operations. A well-defined test scope can guide you all through the journey for delivering a good software product with reduced risks.


2 Answers

David Chelimsky (Rspec's creator) offered up the following example 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 

It's obviously not the same example as yours, but it should give you an idea of how to do it. The relevant thread for context is here: http://groups.google.com/group/rspec/browse_thread/thread/6706c3f2cceef97f

like image 156
samullen Avatar answered Sep 28 '22 10:09

samullen


I'm sure there's a more elegant solution, but I've always just set up some objects that should and shouldn't be in my scope. After calling the scope, I check that the returned has the object that it should, and doesn't have the object that it shouldn't.

If anything, I hope to be enlightened by other answers.

like image 40
theIV Avatar answered Sep 28 '22 08:09

theIV