Created
September 10, 2009 02:33
-
-
Save ryanbriones/184253 to your computer and use it in GitHub Desktop.
testing a chained named scope called within a method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# how do I test this elegantly? | |
class Request < ActiveRecord::Base | |
named_scope :active, :conditions => {:status => 'active'} | |
named_scope :incomplete, :conditions => {:completed_at => nil} | |
named_scope :deliquent, lambda { {:conditions => 10.days.ago} } | |
def self.do_stuff_with_incomplete_requests | |
incomplete_requests = Request.active.incomplete.deliquent(:include => {:user}) | |
incomplete_requests.each do |request| | |
request.do_stuff | |
end | |
end | |
end | |
describe Request, "when doing stuff with incomplete requests" do | |
it "should find all active, incomplete, deliquent requests" do | |
pending("this test should verify that we're finding active, incomplete requests") | |
end | |
it "should find all active, incomplete, deliquent requests...poorly (imho)" do | |
# doing this setup every time is a pain | |
incomplete_mock = mock('incomplete') | |
active_mock = mock('active', :incomplete => incomplete_mock) | |
Request.stub!(:active => active_mock) | |
incomplete_mock.should_receive(:deliquent).with(:include => {:user}).and_return([]) | |
Request.do_stuff_with_incomplete_requests | |
end | |
it "should do stuff with each incomplete request" do | |
mock_request = mock('Request') | |
Request.stub_chain(:active, :incomplete, :deliquent => [mock_request]) | |
mock_request.should_receive(:do_stuff) | |
Request.do_stuff_with_incomplete_requests | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment