dchelimsky (owner)

Fork Of

gist: 184253 by ryanbri... testing a chained named sco...

Revisions

gist: 184265 Download_button fork
public
Public Clone URL: git://gist.github.com/184265.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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