Upgraded Rails and RSpec
[monkeycharger.git] / vendor / plugins / rspec / rspec / spec / spec / mocks / stub_spec.rb
blob88772b73facf5656a9345f8ad2712c26539a77a6
1 require File.dirname(__FILE__) + '/../../spec_helper.rb'
3 module Spec
4   module Mocks
5     describe "A method stub" do
6       before(:each) do
7         @class = Class.new do
8           def self.existing_class_method
9             :original_value
10           end
12           def existing_instance_method
13             :original_value
14           end
15         end
16         @obj = @class.new
17       end
19       it "should allow for a mock expectation to temporarily replace a method stub on a mock" do
20         mock = Spec::Mocks::Mock.new("a mock")
21         mock.stub!(:msg).and_return(:stub_value)
22         mock.should_receive(:msg).with(:arg).and_return(:mock_value)
23         mock.msg(:arg).should equal(:mock_value)
24         mock.msg.should equal(:stub_value)
25         mock.msg.should equal(:stub_value)
26         mock.rspec_verify
27       end
29       it "should allow for a mock expectation to temporarily replace a method stub on a non-mock" do
30         @obj.stub!(:msg).and_return(:stub_value)
31         @obj.should_receive(:msg).with(:arg).and_return(:mock_value)
32         @obj.msg(:arg).should equal(:mock_value)
33         @obj.msg.should equal(:stub_value)
34         @obj.msg.should equal(:stub_value)
35         @obj.rspec_verify
36       end
38       it "should ignore when expected message is not received" do
39         @obj.stub!(:msg)
40         lambda do
41           @obj.rspec_verify
42         end.should_not raise_error
43       end
44       
45       it "should clear itself on rspec_verify" do
46         @obj.stub!(:this_should_go).and_return(:blah)
47         @obj.this_should_go.should == :blah
48         @obj.rspec_verify
49         lambda do
50           @obj.this_should_go
51         end.should raise_error
52       end
54       it "should ignore when expected message is received" do
55         @obj.stub!(:msg)
56         @obj.msg
57         @obj.rspec_verify
58       end
60       it "should ignore when message is received with args" do
61         @obj.stub!(:msg)
62         @obj.msg(:an_arg)
63         @obj.rspec_verify
64       end
66       it "should return expected value when expected message is received" do
67         @obj.stub!(:msg).and_return(:return_value)
68         @obj.msg.should equal(:return_value)
69         @obj.rspec_verify
70       end
71       
72       it "should return values in order to consecutive calls" do
73         return_values = ["1",2,Object.new]
74         @obj.stub!(:msg).and_return(return_values[0],return_values[1],return_values[2])
75         @obj.msg.should == return_values[0]
76         @obj.msg.should == return_values[1]
77         @obj.msg.should == return_values[2]
78       end
80       it "should keep returning last value in consecutive calls" do
81         return_values = ["1",2,Object.new]
82         @obj.stub!(:msg).and_return(return_values[0],return_values[1],return_values[2])
83         @obj.msg.should == return_values[0]
84         @obj.msg.should == return_values[1]
85         @obj.msg.should == return_values[2]
86         @obj.msg.should == return_values[2]
87         @obj.msg.should == return_values[2]
88       end
90       it "should revert to original instance method if existed" do
91         @obj.existing_instance_method.should equal(:original_value)
92         @obj.stub!(:existing_instance_method).and_return(:mock_value)
93         @obj.existing_instance_method.should equal(:mock_value)
94         @obj.rspec_verify
95         @obj.existing_instance_method.should equal(:original_value)
96       end
97       
98       it "should revert to original class method if existed" do
99         @class.existing_class_method.should equal(:original_value)
100         @class.stub!(:existing_class_method).and_return(:mock_value)
101         @class.existing_class_method.should equal(:mock_value)
102         @class.rspec_verify
103         @class.existing_class_method.should equal(:original_value)
104       end
106       it "should support yielding" do
107         @obj.stub!(:method_that_yields).and_yield(:yielded_value)
108         current_value = :value_before
109         @obj.method_that_yields {|val| current_value = val}
110         current_value.should == :yielded_value
111         @obj.rspec_verify
112       end
114       it "should support yielding multiple times" do
115         @obj.stub!(:method_that_yields_multiple_times).and_yield(:yielded_value).
116                                                        and_yield(:another_value)
117         current_value = []
118         @obj.method_that_yields_multiple_times {|val| current_value << val}
119         current_value.should == [:yielded_value, :another_value]
120         @obj.rspec_verify
121       end
123       it "should throw when told to" do
124         @mock.stub!(:something).and_throw(:blech)
125         lambda do
126           @mock.something
127         end.should throw_symbol(:blech)
128       end
129       
130       it "should support overriding w/ a new stub" do
131         @stub.stub!(:existing_instance_method).and_return(:updated_stub_value)
132         @stub.existing_instance_method.should == :updated_stub_value
133       end
134       
135       it "should support stub with" do
136         @stub.stub!(:foo).with("bar")
137         @stub.should_receive(:foo).with("baz")
138         @stub.foo("bar")
139         @stub.foo("baz")
140       end
141     end
142     
143     describe "A method stub with args" do
144       before(:each) do
145         @stub = Object.new
146         @stub.stub!(:foo).with("bar")
147       end
149       it "should not complain if not called" do
150       end
152       it "should not complain if called with arg" do
153         @stub.foo("bar")
154       end
156       it "should complain if called with no arg" do
157         lambda do
158           @stub.foo
159         end.should raise_error
160       end
162       it "should complain if called with other arg" do
163         lambda do
164           @stub.foo("other")
165         end.should raise_error
166       end
168       it "should not complain if also mocked w/ different args" do
169         @stub.should_receive(:foo).with("baz")
170         @stub.foo("bar")
171         @stub.foo("baz")
172       end
174       it "should complain if also mocked w/ different args AND called w/ a 3rd set of args" do
175         @stub.should_receive(:foo).with("baz")
176         @stub.foo("bar")
177         @stub.foo("baz")
178         lambda do
179           @stub.foo("other")
180         end.should raise_error
181       end
182       
183       it "should support options" do
184         @stub.stub!(:foo, :expected_from => "bar")
185       end
186     end
188   end