Upgraded Rails and RSpec
[monkeycharger.git] / vendor / plugins / rspec / spec / spec / mocks / mock_spec.rb
blobbfe36ed57f17a669b7ac3f49bf55036b6684db3c
1 require File.dirname(__FILE__) + '/../../spec_helper'
3 module Spec
4   module Mocks
5     describe "a Mock expectation" do
7       before do
8         @mock = mock("test mock")
9       end
10       
11       after do
12         @mock.rspec_reset
13       end
14       
15       it "should report line number of expectation of unreceived message" do
16         @mock.should_receive(:wont_happen).with("x", 3)
17         #NOTE - this test is quite ticklish because it specifies that
18         #the above statement appears on line 12 of this file.
20         begin
21           @mock.rspec_verify
22           violated
23         rescue MockExpectationError => e
24           e.backtrace[0].should match(/mock_spec\.rb:16/)
25         end
26     
27       end
28       
29       it "should pass when not receiving message specified as not to be received" do
30         @mock.should_not_receive(:not_expected)
31         @mock.rspec_verify
32       end
34       it "should pass when receiving message specified as not to be received with different args" do
35         @mock.should_not_receive(:message).with("unwanted text")
36         @mock.should_receive(:message).with("other text")
37         @mock.message "other text"
38         @mock.rspec_verify
39       end
41       it "should fail when receiving message specified as not to be received" do
42         @mock.should_not_receive(:not_expected)
43         @mock.not_expected
44         begin
45           @mock.rspec_verify
46           violated
47         rescue MockExpectationError => e
48           e.message.should == "Mock 'test mock' expected :not_expected with (any args) 0 times, but received it once"
49         end
50       end
52       it "should fail when receiving message specified as not to be received with args" do
53         @mock.should_not_receive(:not_expected).with("unexpected text")
54         @mock.not_expected("unexpected text")
55         begin
56           @mock.rspec_verify
57           violated
58         rescue MockExpectationError => e
59           e.message.should == "Mock 'test mock' expected :not_expected with (\"unexpected text\") 0 times, but received it once"
60         end
61       end
63       it "should pass when receiving message specified as not to be received with wrong args" do
64         @mock.should_not_receive(:not_expected).with("unexpected text")
65         @mock.not_expected "really unexpected text"
66         @mock.rspec_verify
67       end
69       it "should allow block to calculate return values" do
70         @mock.should_receive(:something).with("a","b","c").and_return { |a,b,c| c+b+a }
71         @mock.something("a","b","c").should == "cba"
72         @mock.rspec_verify
73       end
75       it "should allow parameter as return value" do
76         @mock.should_receive(:something).with("a","b","c").and_return("booh")
77         @mock.something("a","b","c").should == "booh"
78         @mock.rspec_verify
79       end
81       it "should return nil if no return value set" do
82         @mock.should_receive(:something).with("a","b","c")
83         @mock.something("a","b","c").should be_nil
84         @mock.rspec_verify
85       end
87       it "should raise exception if args dont match when method called" do
88         @mock.should_receive(:something).with("a","b","c").and_return("booh")
89         begin
90           @mock.something("a","d","c")
91           violated
92         rescue MockExpectationError => e
93           e.message.should == "Mock 'test mock' expected :something with (\"a\", \"b\", \"c\") but received it with (\"a\", \"d\", \"c\")"
94         end
95       end
96      
97       it "should fail if unexpected method called" do
98         begin
99           @mock.something("a","b","c")
100           violated
101         rescue MockExpectationError => e
102           e.message.should == "Mock 'test mock' received unexpected message :something with (\"a\", \"b\", \"c\")"
103         end
104       end
105   
106       it "should use block for expectation if provided" do
107         @mock.should_receive(:something) do | a, b |
108           a.should == "a"
109           b.should == "b"
110           "booh"
111         end
112         @mock.something("a", "b").should == "booh"
113         @mock.rspec_verify
114       end
115   
116       it "should fail if expectation block fails" do
117         @mock.should_receive(:something) {| bool | bool.should be_true}
118         begin
119           @mock.something false
120         rescue MockExpectationError => e
121           e.message.should match(/Mock 'test mock' received :something but passed block failed with: expected true, got false/)
122         end
123       end
124   
125       it "should fail when method defined as never is received" do
126         @mock.should_receive(:not_expected).never
127         begin
128           @mock.not_expected
129         rescue MockExpectationError => e
130           e.message.should == "Mock 'test mock' expected :not_expected 0 times, but received it 1 times"
131         end
132       end
133       
134       it "should raise when told to" do
135         @mock.should_receive(:something).and_raise(RuntimeError)
136         lambda do
137           @mock.something
138         end.should raise_error(RuntimeError)
139       end
141       it "should raise passed an Exception instance" do
142         error = RuntimeError.new("error message")
143         @mock.should_receive(:something).and_raise(error)
144         begin
145           @mock.something
146         rescue RuntimeError => e
147           e.message.should eql("error message")
148         end
149       end
151       it "should raise RuntimeError with passed message" do
152         @mock.should_receive(:something).and_raise("error message")
153         begin
154           @mock.something
155         rescue RuntimeError => e
156           e.message.should eql("error message")
157         end
158       end
160       it "should not raise when told to if args dont match" do
161         @mock.should_receive(:something).with(2).and_raise(RuntimeError)
162         lambda do
163           @mock.something 1
164         end.should raise_error(MockExpectationError)
165       end
167       it "should throw when told to" do
168         @mock.should_receive(:something).and_throw(:blech)
169         lambda do
170           @mock.something
171         end.should throw_symbol(:blech)
172       end
174       it "should raise when explicit return and block constrained" do
175         lambda do
176           @mock.should_receive(:fruit) do |colour|
177             :strawberry
178           end.and_return :apple
179         end.should raise_error(AmbiguousReturnError)
180       end
181       
182       it "should ignore args on any args" do
183         @mock.should_receive(:something).at_least(:once).with(any_args)
184         @mock.something
185         @mock.something 1
186         @mock.something "a", 2
187         @mock.something [], {}, "joe", 7
188         @mock.rspec_verify
189       end
190       
191       it "should fail on no args if any args received" do
192         @mock.should_receive(:something).with(no_args())
193         begin
194           @mock.something 1
195         rescue MockExpectationError => e
196           e.message.should == "Mock 'test mock' expected :something with (no args) but received it with (1)"
197         end
198       end
199       
200       it "should fail when args are expected but none are received" do
201         @mock.should_receive(:something).with(1)
202         begin
203           @mock.something
204         rescue MockExpectationError => e
205           e.message.should == "Mock 'test mock' expected :something with (1) but received it with (no args)"
206         end
207       end
209       it "should yield 0 args to blocks that take a variable number of arguments" do
210         @mock.should_receive(:yield_back).with(no_args()).once.and_yield
211         a = nil
212         @mock.yield_back {|*a|}
213         a.should == []
214         @mock.rspec_verify
215       end
217       it "should yield one arg to blocks that take a variable number of arguments" do
218         @mock.should_receive(:yield_back).with(no_args()).once.and_yield(99)
219         a = nil
220         @mock.yield_back {|*a|}
221         a.should == [99]
222         @mock.rspec_verify
223       end
225       it "should yield many args to blocks that take a variable number of arguments" do
226         @mock.should_receive(:yield_back).with(no_args()).once.and_yield(99, 27, "go")
227         a = nil
228         @mock.yield_back {|*a|}
229         a.should == [99, 27, "go"]
230         @mock.rspec_verify
231       end
233       it "should yield single value" do
234         @mock.should_receive(:yield_back).with(no_args()).once.and_yield(99)
235         a = nil
236         @mock.yield_back {|a|}
237         a.should == 99
238         @mock.rspec_verify
239       end
241       it "should yield two values" do
242         @mock.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
243         a, b = nil
244         @mock.yield_back {|a,b|}
245         a.should == 'wha'
246         b.should == 'zup'
247         @mock.rspec_verify
248       end
250       it "should fail when calling yielding method with wrong arity" do
251         @mock.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
252           begin
253           @mock.yield_back {|a|}
254         rescue MockExpectationError => e
255           e.message.should == "Mock 'test mock' yielded |\"wha\", \"zup\"| to block with arity of 1"
256         end
257       end
259       it "should fail when calling yielding method without block" do
260         @mock.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
261         begin
262           @mock.yield_back
263         rescue MockExpectationError => e
264           e.message.should == "Mock 'test mock' asked to yield |\"wha\", \"zup\"| but no block was passed"
265         end
266       end
267       
268       it "should be able to mock send" do
269         @mock.should_receive(:send).with(any_args)
270         @mock.send 'hi'
271         @mock.rspec_verify
272       end
273       
274       it "should be able to raise from method calling yielding mock" do
275         @mock.should_receive(:yield_me).and_yield 44
276         
277         lambda do
278           @mock.yield_me do |x|
279             raise "Bang"
280           end
281         end.should raise_error(StandardError)
283         @mock.rspec_verify
284       end
285       
286       # TODO - this is failing, but not if you run the file w/ --reverse - weird!!!!!!
287       # specify "should clear expectations after verify" do
288       #   @mock.should_receive(:foobar)
289       #   @mock.foobar
290       #   @mock.rspec_verify
291       #   begin
292       #     @mock.foobar
293       #   rescue MockExpectationError => e
294       #     e.message.should == "Mock 'test mock' received unexpected message :foobar with (no args)"
295       #   end
296       # end
297       
298       it "should restore objects to their original state on rspec_reset" do
299         mock = mock("this is a mock")
300         mock.should_receive(:blah)
301         mock.rspec_reset
302         mock.rspec_verify #should throw if reset didn't work
303       end
305     end
307     describe "a mock message receiving a block" do
308       before(:each) do
309         @mock = mock("mock")
310         @calls = 0
311       end
312       
313       def add_call
314         @calls = @calls + 1
315       end
316       
317       it "should call the block after #should_receive" do
318         @mock.should_receive(:foo) { add_call }
320         @mock.foo
322         @calls.should == 1
323       end
325       it "should call the block after #once" do
326         @mock.should_receive(:foo).once { add_call }
328         @mock.foo
330         @calls.should == 1
331       end
333       it "should call the block after #twice" do
334         @mock.should_receive(:foo).twice { add_call }
336         @mock.foo
337         @mock.foo
339         @calls.should == 2
340       end
342       it "should call the block after #times" do
343         @mock.should_receive(:foo).exactly(10).times { add_call }
344         
345         (1..10).each { @mock.foo }
347         @calls.should == 10
348       end
350       it "should call the block after #any_number_of_times" do
351         @mock.should_receive(:foo).any_number_of_times { add_call }
352         
353         (1..7).each { @mock.foo }
355         @calls.should == 7
356       end
358       it "should call the block after #with" do
359         @mock.should_receive(:foo).with(:arg) { add_call }
360         
361         @mock.foo(:arg)
363         @calls.should == 1
364       end
366       it "should call the block after #ordered" do
367         @mock.should_receive(:foo).ordered { add_call }
368         @mock.should_receive(:bar).ordered { add_call }
369         
370         @mock.foo
371         @mock.bar
373         @calls.should == 2
374       end
375     end
376   end