dchelimsky (owner)

Revisions

gist: 151226 Download_button fork
public
Public Clone URL: git://gist.github.com/151226.git
Embed All Files: show embed
very_invasive_spec.rb #
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class Thing
  def self.find_something(inputs)
    # .. do stuff here
  end
 
  def self.add_something(inputs)
    # .. do stuff here
  end
 
  def self.find_or_add_something(inputs)
    if x = self.find_something(inputs)
      x
    else
      self.add_something(inputs)
      self.find_something(inputs)
    end
  end
end
 
describe Thing do
  describe "#find_or_add_something" do
    def thing
      @thing ||= stub()
    end
    
    before(:each) do
      Thing.stub(:find_something)
      Thing.stub(:add_something)
    end
    
    it "passes inputs to find_something" do
      Thing.should_receive(:find_something).
        with(:this => 'data').
        and_return(thing)
      Thing.find_or_add_something(:this => 'data')
    end
    
    context "when find_something returns a Thing" do
      it "returns the Thing" do
        Thing.stub(:find_something).and_return(thing)
        Thing.find_or_add_something(:this => 'data').
          should equal(thing)
      end
    end
    
    context "when find_something returns nil" do
      it "calls add_something with the given inputs" do
        Thing.should_receive(:add_something).
          with(:this => 'data')
        Thing.find_or_add_something(:this => 'data')
      end
      
      it "calls find_something with the given inputs" do
        Thing.should_receive(:find_something).
          with(:this => 'data')
        Thing.find_or_add_something(:this => 'data')
      end
      
      it "returns the result of find_something" do
        Thing.stub(:find_something).and_return(nil, thing)
        Thing.find_or_add_something(:this => 'data').
          should equal(thing)
      end
    end
  end
end