Updated to latest rspec
[gitorious:georgyos-clone.git] / vendor / plugins / rspec / spec / spec / mocks / failing_mock_argument_constraints_spec.rb
1 require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
3 module Spec
4   module Mocks
5     describe "failing MockArgumentConstraints" do
6       before(:each) do
7         @mock = mock("test mock")
8         @reporter = Mock.new("reporter", :null_object => true)
9       end
10       
11       after(:each) do
12         @mock.rspec_reset
13       end
14
15       it "should reject non boolean" do
16         @mock.should_receive(:random_call).with(boolean())
17         lambda do
18           @mock.random_call("false")
19         end.should raise_error(MockExpectationError)
20       end
21
22       it "should reject non numeric" do
23         @mock.should_receive(:random_call).with(an_instance_of(Numeric))
24         lambda do
25           @mock.random_call("1")
26         end.should raise_error(MockExpectationError)
27       end
28       
29       it "should reject non string" do
30         @mock.should_receive(:random_call).with(an_instance_of(String))
31         lambda do
32           @mock.random_call(123)
33         end.should raise_error(MockExpectationError)
34       end
35       
36       it "should reject goose when expecting a duck" do
37         @mock.should_receive(:random_call).with(duck_type(:abs, :div))
38         lambda { @mock.random_call("I don't respond to :abs or :div") }.should raise_error(MockExpectationError)
39       end
40
41       it "should fail if regexp does not match submitted string" do
42         @mock.should_receive(:random_call).with(/bcd/)
43         lambda { @mock.random_call("abc") }.should raise_error(MockExpectationError)
44       end
45       
46       it "should fail if regexp does not match submitted regexp" do
47         @mock.should_receive(:random_call).with(/bcd/)
48         lambda { @mock.random_call(/bcde/) }.should raise_error(MockExpectationError)
49       end
50       
51       it "should fail for a hash w/ wrong values" do
52         @mock.should_receive(:random_call).with(:a => "b", :c => "d")
53         lambda do
54           @mock.random_call(:a => "b", :c => "e")
55         end.should raise_error(MockExpectationError, /Mock 'test mock' expected :random_call with \(\{(:a=>\"b\", :c=>\"d\"|:c=>\"d\", :a=>\"b\")\}\) but received it with \(\{(:a=>\"b\", :c=>\"e\"|:c=>\"e\", :a=>\"b\")\}\)/)
56       end
57       
58       it "should fail for a hash w/ wrong keys" do
59         @mock.should_receive(:random_call).with(:a => "b", :c => "d")
60         lambda do
61           @mock.random_call("a" => "b", "c" => "d")
62         end.should raise_error(MockExpectationError, /Mock 'test mock' expected :random_call with \(\{(:a=>\"b\", :c=>\"d\"|:c=>\"d\", :a=>\"b\")\}\) but received it with \(\{(\"a\"=>\"b\", \"c\"=>\"d\"|\"c\"=>\"d\", \"a\"=>\"b\")\}\)/)
63       end
64       
65       it "should match against a Matcher" do
66         lambda do
67           @mock.should_receive(:msg).with(equal(3))
68           @mock.msg(37)
69         end.should raise_error(MockExpectationError, "Mock 'test mock' expected :msg with (equal 3) but received it with (37)")
70       end
71       
72       it "should fail no_args with one arg" do
73         lambda do
74           @mock.should_receive(:msg).with(no_args)
75           @mock.msg(37)
76         end.should raise_error(MockExpectationError, "Mock 'test mock' expected :msg with (no args) but received it with (37)")
77       end
78       
79       it "should fail hash_including with missing key" do
80          lambda do
81            @mock.should_receive(:msg).with(hash_including(:a => 1))
82            @mock.msg({})
83          end.should raise_error(MockExpectationError, "Mock 'test mock' expected :msg with (hash_including(:a=>1)) but received it with ({})")
84       end
85             
86     end
87       
88     describe "failing deprecated MockArgumentConstraints" do
89       before(:each) do
90         @mock = mock("test mock")
91         @reporter = Mock.new("reporter", :null_object => true)
92         Kernel.stub!(:warn)
93       end
94
95       after(:each) do
96         @mock.rspec_reset
97       end
98
99       it "should reject non boolean" do
100         @mock.should_receive(:random_call).with(:boolean)
101         lambda do
102           @mock.random_call("false")
103         end.should raise_error(MockExpectationError)
104       end
105       
106       it "should reject non numeric" do
107         @mock.should_receive(:random_call).with(:numeric)
108         lambda do
109           @mock.random_call("1")
110         end.should raise_error(MockExpectationError)
111       end
112       
113       it "should reject non string" do
114         @mock.should_receive(:random_call).with(:string)
115         lambda do
116           @mock.random_call(123)
117         end.should raise_error(MockExpectationError)
118       end
119       
120
121     end
122   end
123 end