Skip to content

Instantly share code, notes, and snippets.

@dchelimsky
Forked from ryanbriones/gist:184253
Created September 10, 2009 03:00
Show Gist options
  • Save dchelimsky/184265 to your computer and use it in GitHub Desktop.
Save dchelimsky/184265 to your computer and use it in GitHub Desktop.
# 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