rspec-mocks 2.6.0 → 2.7.0.rc1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/features/Scope.md +17 -0
- data/features/argument_matchers/README.md +27 -0
- data/features/argument_matchers/explicit.feature +60 -0
- data/features/argument_matchers/general_matchers.feature +85 -0
- data/features/argument_matchers/type_matchers.feature +25 -0
- data/features/message_expectations/any_instance.feature +2 -0
- data/features/message_expectations/receive_counts.feature +209 -0
- data/features/method_stubs/README.md +6 -10
- data/features/method_stubs/any_instance.feature +111 -1
- data/features/method_stubs/simple_return_value.feature +34 -25
- data/features/method_stubs/stub_implementation.feature +1 -1
- data/features/method_stubs/to_ary.feature +12 -10
- data/features/support/env.rb +1 -1
- data/lib/rspec/mocks.rb +1 -1
- data/lib/rspec/mocks/any_instance.rb +8 -235
- data/lib/rspec/mocks/any_instance/chain.rb +49 -0
- data/lib/rspec/mocks/any_instance/expectation_chain.rb +33 -0
- data/lib/rspec/mocks/any_instance/message_chains.rb +48 -0
- data/lib/rspec/mocks/any_instance/recorder.rb +162 -0
- data/lib/rspec/mocks/any_instance/stub_chain.rb +35 -0
- data/lib/rspec/mocks/any_instance/stub_chain_chain.rb +34 -0
- data/lib/rspec/mocks/argument_expectation.rb +1 -1
- data/lib/rspec/mocks/extensions/marshal.rb +1 -1
- data/lib/rspec/mocks/extensions/psych.rb +1 -1
- data/lib/rspec/mocks/methods.rb +1 -1
- data/lib/rspec/mocks/mock.rb +2 -2
- data/lib/rspec/mocks/proxy.rb +1 -1
- data/lib/rspec/mocks/version.rb +1 -1
- data/spec/rspec/mocks/any_instance/message_chains_spec.rb +40 -0
- data/spec/rspec/mocks/any_instance_spec.rb +147 -2
- data/spec/rspec/mocks/any_number_of_times_spec.rb +2 -2
- data/spec/rspec/mocks/argument_expectation_spec.rb +15 -3
- data/spec/rspec/mocks/at_least_spec.rb +1 -1
- data/spec/rspec/mocks/at_most_spec.rb +1 -1
- data/spec/rspec/mocks/bug_report_10263_spec.rb +1 -1
- data/spec/rspec/mocks/bug_report_7611_spec.rb +1 -1
- data/spec/rspec/mocks/bug_report_8165_spec.rb +2 -2
- data/spec/rspec/mocks/bug_report_957_spec.rb +2 -2
- data/spec/rspec/mocks/failing_argument_matchers_spec.rb +1 -1
- data/spec/rspec/mocks/hash_including_matcher_spec.rb +11 -11
- data/spec/rspec/mocks/hash_not_including_matcher_spec.rb +6 -6
- data/spec/rspec/mocks/mock_spec.rb +49 -43
- data/spec/rspec/mocks/multiple_return_value_spec.rb +26 -26
- data/spec/rspec/mocks/partial_mock_spec.rb +3 -2
- data/spec/rspec/mocks/partial_mock_using_mocks_directly_spec.rb +1 -0
- data/spec/rspec/mocks/passing_argument_matchers_spec.rb +3 -3
- data/spec/rspec/mocks/precise_counts_spec.rb +1 -1
- data/spec/rspec/mocks/serialization_spec.rb +7 -2
- data/spec/rspec/mocks/stub_implementation_spec.rb +6 -6
- data/spec/rspec/mocks/stub_spec.rb +6 -6
- data/spec/rspec/mocks/to_ary_spec.rb +9 -0
- metadata +54 -47
- data/.autotest +0 -7
- data/.document +0 -5
- data/.gitignore +0 -10
- data/.travis.yml +0 -7
- data/Gemfile +0 -40
- data/Guardfile +0 -8
- data/License.txt +0 -22
- data/Rakefile +0 -75
- data/autotest/discover.rb +0 -1
- data/cucumber.yml +0 -2
- data/features/.nav +0 -17
- data/features/Changelog.md +0 -80
- data/rspec-mocks.gemspec +0 -25
- data/specs.watchr +0 -57
@@ -1,3 +1,4 @@
|
|
1
|
+
|
1
2
|
require 'spec_helper'
|
2
3
|
|
3
4
|
module RSpec
|
@@ -8,6 +9,7 @@ module RSpec
|
|
8
9
|
let(:klass) do
|
9
10
|
Class.new do
|
10
11
|
def existing_method; :existing_method_return_value; end
|
12
|
+
def existing_method_with_arguments(arg_one, arg_two = nil); :existing_method_with_arguments_return_value; end
|
11
13
|
def another_existing_method; end
|
12
14
|
end
|
13
15
|
end
|
@@ -31,7 +33,13 @@ module RSpec
|
|
31
33
|
lambda{ klass.any_instance.stub(:foo).and_yield(1).with("1") }.should raise_error(NoMethodError)
|
32
34
|
end
|
33
35
|
end
|
34
|
-
|
36
|
+
|
37
|
+
context "#stub_chain" do
|
38
|
+
it "raises an error if 'stub_chain' follows 'any_instance'" do
|
39
|
+
lambda{ klass.any_instance.and_return("1").stub_chain(:foo, :bar) }.should raise_error(NoMethodError)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
35
43
|
context "#should_receive" do
|
36
44
|
it "raises an error if 'should_receive' follows 'with'" do
|
37
45
|
lambda{ klass.any_instance.with("1").should_receive(:foo) }.should raise_error(NoMethodError)
|
@@ -48,13 +56,44 @@ module RSpec
|
|
48
56
|
end
|
49
57
|
end
|
50
58
|
end
|
51
|
-
|
59
|
+
|
52
60
|
context "with #stub" do
|
53
61
|
it "does not suppress an exception when a method that doesn't exist is invoked" do
|
54
62
|
klass.any_instance.stub(:foo)
|
55
63
|
lambda{ klass.new.bar }.should raise_error(NoMethodError)
|
56
64
|
end
|
65
|
+
|
66
|
+
context 'multiple methods' do
|
67
|
+
it "allows multiple methods to be stubbed in a single invocation" do
|
68
|
+
klass.any_instance.stub(:foo => 'foo', :bar => 'bar')
|
69
|
+
instance = klass.new
|
70
|
+
instance.foo.should eq('foo')
|
71
|
+
instance.bar.should eq('bar')
|
72
|
+
end
|
73
|
+
|
74
|
+
it "adheres to the contract of multiple method stubbing withou any instance" do
|
75
|
+
Object.new.stub(:foo => 'foo', :bar => 'bar').should eq(:foo => 'foo', :bar => 'bar')
|
76
|
+
klass.any_instance.stub(:foo => 'foo', :bar => 'bar').should eq(:foo => 'foo', :bar => 'bar')
|
77
|
+
end
|
78
|
+
|
79
|
+
context "allows a chain of methods to be stubbed using #stub_chain" do
|
80
|
+
it "given symbols representing the methods" do
|
81
|
+
klass.any_instance.stub_chain(:one, :two, :three).and_return(:four)
|
82
|
+
klass.new.one.two.three.should eq(:four)
|
83
|
+
end
|
57
84
|
|
85
|
+
it "given a hash as the last argument uses the value as the expected return value" do
|
86
|
+
klass.any_instance.stub_chain(:one, :two, :three => :four)
|
87
|
+
klass.new.one.two.three.should eq(:four)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "given a string of '.' separated method names representing the chain" do
|
91
|
+
klass.any_instance.stub_chain('one.two.three').and_return(:four)
|
92
|
+
klass.new.one.two.three.should eq(:four)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
58
97
|
context "behaves as 'every instance'" do
|
59
98
|
it "stubs every instance in the spec" do
|
60
99
|
klass.any_instance.stub(:foo).and_return(result = Object.new)
|
@@ -68,6 +107,43 @@ module RSpec
|
|
68
107
|
instance.foo.should eq(result)
|
69
108
|
end
|
70
109
|
end
|
110
|
+
|
111
|
+
context "with argument matching" do
|
112
|
+
before do
|
113
|
+
klass.any_instance.stub(:foo).with(:param_one, :param_two).and_return(:result_one)
|
114
|
+
klass.any_instance.stub(:foo).with(:param_three, :param_four).and_return(:result_two)
|
115
|
+
end
|
116
|
+
|
117
|
+
it "returns the stubbed value when arguments match" do
|
118
|
+
instance = klass.new
|
119
|
+
instance.foo(:param_one, :param_two).should eq(:result_one)
|
120
|
+
instance.foo(:param_three, :param_four).should eq(:result_two)
|
121
|
+
end
|
122
|
+
|
123
|
+
it "fails the spec with an expectation error when the arguments do not match" do
|
124
|
+
expect do
|
125
|
+
klass.new.foo(:param_one, :param_three)
|
126
|
+
end.to(raise_error(RSpec::Mocks::MockExpectationError))
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
context "with multiple stubs" do
|
131
|
+
before do
|
132
|
+
klass.any_instance.stub(:foo).and_return(1)
|
133
|
+
klass.any_instance.stub(:bar).and_return(2)
|
134
|
+
end
|
135
|
+
|
136
|
+
it "stubs a method" do
|
137
|
+
instance = klass.new
|
138
|
+
instance.foo.should eq(1)
|
139
|
+
instance.bar.should eq(2)
|
140
|
+
end
|
141
|
+
|
142
|
+
it "returns the same value for calls on different instances" do
|
143
|
+
klass.new.foo.should eq(klass.new.foo)
|
144
|
+
klass.new.bar.should eq(klass.new.bar)
|
145
|
+
end
|
146
|
+
end
|
71
147
|
|
72
148
|
context "with #and_return" do
|
73
149
|
it "stubs a method that doesn't exist" do
|
@@ -159,7 +235,35 @@ module RSpec
|
|
159
235
|
end
|
160
236
|
end
|
161
237
|
end
|
238
|
+
|
239
|
+
context "unstub implementation" do
|
240
|
+
it "replaces the stubbed method with the original method" do
|
241
|
+
klass.any_instance.stub(:existing_method)
|
242
|
+
klass.any_instance.unstub(:existing_method)
|
243
|
+
klass.new.existing_method.should eq(:existing_method_return_value)
|
244
|
+
end
|
245
|
+
|
246
|
+
it "removes all stubs with the supplied method name" do
|
247
|
+
klass.any_instance.stub(:existing_method).with(1)
|
248
|
+
klass.any_instance.stub(:existing_method).with(2)
|
249
|
+
klass.any_instance.unstub(:existing_method)
|
250
|
+
klass.new.existing_method.should eq(:existing_method_return_value)
|
251
|
+
end
|
252
|
+
|
253
|
+
it "does not remove any expectations with the same method name" do
|
254
|
+
klass.any_instance.should_receive(:existing_method_with_arguments).with(3).and_return(:three)
|
255
|
+
klass.any_instance.stub(:existing_method_with_arguments).with(1)
|
256
|
+
klass.any_instance.stub(:existing_method_with_arguments).with(2)
|
257
|
+
klass.any_instance.unstub(:existing_method_with_arguments)
|
258
|
+
klass.new.existing_method_with_arguments(3).should eq :three
|
259
|
+
end
|
162
260
|
|
261
|
+
it "raises a MockExpectationError if the method has not been stubbed" do
|
262
|
+
lambda do
|
263
|
+
klass.any_instance.unstub(:existing_method)
|
264
|
+
end.should raise_error(RSpec::Mocks::MockExpectationError, 'The method `existing_method` was not stubbed or was already unstubbed')
|
265
|
+
end
|
266
|
+
end
|
163
267
|
context "with #should_receive" do
|
164
268
|
let(:foo_expectation_error_message) { 'Exactly one instance should have received the following message(s) but didn\'t: foo' }
|
165
269
|
let(:existing_method_expectation_error_message) { 'Exactly one instance should have received the following message(s) but didn\'t: existing_method' }
|
@@ -292,6 +396,47 @@ module RSpec
|
|
292
396
|
end
|
293
397
|
end
|
294
398
|
|
399
|
+
context "with argument matching" do
|
400
|
+
before do
|
401
|
+
klass.any_instance.should_receive(:foo).with(:param_one, :param_two).and_return(:result_one)
|
402
|
+
klass.any_instance.should_receive(:foo).with(:param_three, :param_four).and_return(:result_two)
|
403
|
+
end
|
404
|
+
|
405
|
+
it "returns the expected value when arguments match" do
|
406
|
+
instance = klass.new
|
407
|
+
instance.foo(:param_one, :param_two).should eq(:result_one)
|
408
|
+
instance.foo(:param_three, :param_four).should eq(:result_two)
|
409
|
+
end
|
410
|
+
|
411
|
+
it "fails when the arguments match but different instances are used" do
|
412
|
+
instances = Array.new(2) { klass.new }
|
413
|
+
expect do
|
414
|
+
instances[0].foo(:param_one, :param_two).should eq(:result_one)
|
415
|
+
instances[1].foo(:param_three, :param_four).should eq(:result_two)
|
416
|
+
end.to raise_error(RSpec::Mocks::MockExpectationError)
|
417
|
+
|
418
|
+
# ignore the fact that should_receive expectations were not met
|
419
|
+
instances.each { |instance| instance.rspec_reset }
|
420
|
+
end
|
421
|
+
|
422
|
+
it "is not affected by the invocation of existing methods on other instances" do
|
423
|
+
klass.new.existing_method_with_arguments(:param_one, :param_two).should eq(:existing_method_with_arguments_return_value)
|
424
|
+
instance = klass.new
|
425
|
+
instance.foo(:param_one, :param_two).should eq(:result_one)
|
426
|
+
instance.foo(:param_three, :param_four).should eq(:result_two)
|
427
|
+
end
|
428
|
+
|
429
|
+
it "fails when arguments do not match" do
|
430
|
+
instance = klass.new
|
431
|
+
expect do
|
432
|
+
instance.foo(:param_one, :param_three)
|
433
|
+
end.to raise_error(RSpec::Mocks::MockExpectationError)
|
434
|
+
|
435
|
+
# ignore the fact that should_receive expectations were not met
|
436
|
+
instance.rspec_reset
|
437
|
+
end
|
438
|
+
end
|
439
|
+
|
295
440
|
context "message count" do
|
296
441
|
context "the 'once' constraint" do
|
297
442
|
it "passes for one invocation" do
|
@@ -24,7 +24,7 @@ describe "AnyNumberOfTimes" do
|
|
24
24
|
it "returns the mocked value when called after a similar stub" do
|
25
25
|
@mock.stub(:message).and_return :stub_value
|
26
26
|
@mock.should_receive(:message).any_number_of_times.and_return(:mock_value)
|
27
|
-
@mock.message.should
|
28
|
-
@mock.message.should
|
27
|
+
@mock.message.should eq :mock_value
|
28
|
+
@mock.message.should eq :mock_value
|
29
29
|
end
|
30
30
|
end
|
@@ -3,12 +3,23 @@ require 'spec_helper'
|
|
3
3
|
module RSpec
|
4
4
|
module Mocks
|
5
5
|
describe ArgumentExpectation do
|
6
|
-
|
6
|
+
|
7
|
+
it "considers an object that responds to #matches? and #failure_message_for_should to be a matcher" do
|
7
8
|
argument_expecatation = RSpec::Mocks::ArgumentExpectation.new
|
8
9
|
obj = double("matcher")
|
9
10
|
obj.stub(:respond_to?).with(:__rspec_double_acting_as_null_object?).and_return(false)
|
10
11
|
obj.stub(:respond_to?).with(:matches?).and_return(true)
|
11
|
-
obj.stub(:respond_to?).with(:
|
12
|
+
obj.stub(:respond_to?).with(:failure_message_for_should).and_return(true)
|
13
|
+
argument_expecatation.is_matcher?(obj).should be_true
|
14
|
+
end
|
15
|
+
|
16
|
+
it "considers an object that responds to #matches? and #failure_message to be a matcher for backward compatibility" do
|
17
|
+
argument_expecatation = RSpec::Mocks::ArgumentExpectation.new
|
18
|
+
obj = double("matcher")
|
19
|
+
obj.stub(:respond_to?).with(:__rspec_double_acting_as_null_object?).and_return(false)
|
20
|
+
obj.stub(:respond_to?).with(:matches?).and_return(true)
|
21
|
+
obj.stub(:respond_to?).with(:failure_message_for_should).and_return(false)
|
22
|
+
obj.stub(:respond_to?).with(:failure_message).and_return(true)
|
12
23
|
argument_expecatation.is_matcher?(obj).should be_true
|
13
24
|
end
|
14
25
|
|
@@ -17,7 +28,8 @@ module RSpec
|
|
17
28
|
obj = double("matcher")
|
18
29
|
obj.stub(:respond_to?).with(:__rspec_double_acting_as_null_object?).and_return(false)
|
19
30
|
obj.stub(:respond_to?).with(:matches?).and_return(true)
|
20
|
-
obj.stub(:respond_to?).with(:
|
31
|
+
obj.stub(:respond_to?).with(:failure_message_for_should).and_return(false)
|
32
|
+
obj.stub(:respond_to?).with(:failure_message).and_return(false)
|
21
33
|
argument_expecatation.is_matcher?(obj).should be_false
|
22
34
|
end
|
23
35
|
end
|
@@ -95,7 +95,7 @@ module RSpec
|
|
95
95
|
|
96
96
|
it "returns the value given by a block when the at least once method is called" do
|
97
97
|
@mock.should_receive(:to_s).at_least(:once) { "testing" }
|
98
|
-
@mock.to_s.should
|
98
|
+
@mock.to_s.should eq "testing"
|
99
99
|
@mock.rspec_verify
|
100
100
|
end
|
101
101
|
end
|
@@ -91,7 +91,7 @@ module RSpec
|
|
91
91
|
|
92
92
|
it "returns the value given by a block when the at most once method is called" do
|
93
93
|
@mock.should_receive(:to_s).at_most(:once) { "testing" }
|
94
|
-
@mock.to_s.should
|
94
|
+
@mock.to_s.should eq "testing"
|
95
95
|
@mock.rspec_verify
|
96
96
|
end
|
97
97
|
end
|
@@ -18,7 +18,7 @@ describe "Double" do
|
|
18
18
|
begin
|
19
19
|
test_double.foobar
|
20
20
|
rescue Exception => e
|
21
|
-
e.message.should
|
21
|
+
e.message.should eq "Double received unexpected message :foobar with (no args)"
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
@@ -17,7 +17,7 @@ describe "An object where respond_to? is true and does not have method" do
|
|
17
17
|
obj.should_receive(:respond_to?).with(:foobar).and_return(true)
|
18
18
|
obj.should_receive(:foobar).and_return(:baz)
|
19
19
|
obj.respond_to?(:foobar).should be_true
|
20
|
-
obj.foobar.should
|
20
|
+
obj.foobar.should eq :baz
|
21
21
|
end
|
22
22
|
|
23
23
|
it "does not raise an exception for mock" do
|
@@ -25,7 +25,7 @@ describe "An object where respond_to? is true and does not have method" do
|
|
25
25
|
obj.should_receive(:respond_to?).with(:foobar).and_return(true)
|
26
26
|
obj.should_receive(:foobar).and_return(:baz)
|
27
27
|
obj.respond_to?(:foobar).should be_true
|
28
|
-
obj.foobar.should
|
28
|
+
obj.foobar.should eq :baz
|
29
29
|
end
|
30
30
|
|
31
31
|
end
|
@@ -11,11 +11,11 @@ module RSpec
|
|
11
11
|
end
|
12
12
|
|
13
13
|
it "returns the value for the stub on the base class" do
|
14
|
-
@base_class.find.should
|
14
|
+
@base_class.find.should eq "stubbed_value"
|
15
15
|
end
|
16
16
|
|
17
17
|
it "returns the value for the descendent class" do
|
18
|
-
@concrete_class.find.should
|
18
|
+
@concrete_class.find.should eq "stubbed_value"
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
@@ -85,7 +85,7 @@ module RSpec
|
|
85
85
|
|
86
86
|
it "fails with block matchers" do
|
87
87
|
expect do
|
88
|
-
@double.should_receive(:msg).with {|arg| arg.should
|
88
|
+
@double.should_receive(:msg).with {|arg| arg.should eq :received }
|
89
89
|
@double.msg :no_msg_for_you
|
90
90
|
end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected: :received.*\s*.*got: :no_msg_for_you/)
|
91
91
|
end
|
@@ -6,51 +6,51 @@ module RSpec
|
|
6
6
|
describe HashIncludingMatcher do
|
7
7
|
|
8
8
|
it "describes itself properly" do
|
9
|
-
HashIncludingMatcher.new(:a => 1).description.should
|
9
|
+
HashIncludingMatcher.new(:a => 1).description.should eq "hash_including(:a=>1)"
|
10
10
|
end
|
11
11
|
|
12
12
|
describe "passing" do
|
13
13
|
it "matches the same hash" do
|
14
|
-
hash_including(:a => 1).should
|
14
|
+
hash_including(:a => 1).should eq({:a => 1})
|
15
15
|
end
|
16
16
|
|
17
17
|
it "matches a hash with extra stuff" do
|
18
|
-
hash_including(:a => 1).should
|
18
|
+
hash_including(:a => 1).should eq({:a => 1, :b => 2})
|
19
19
|
end
|
20
20
|
|
21
21
|
describe "when matching against other matchers" do
|
22
22
|
it "matches an int against anything()" do
|
23
|
-
hash_including(:a => anything, :b => 2).should
|
23
|
+
hash_including(:a => anything, :b => 2).should eq({:a => 1, :b => 2})
|
24
24
|
end
|
25
25
|
|
26
26
|
it "matches a string against anything()" do
|
27
|
-
hash_including(:a => anything, :b => 2).should
|
27
|
+
hash_including(:a => anything, :b => 2).should eq({:a => "1", :b => 2})
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
31
|
describe "when passed only keys or keys mixed with key/value pairs" do
|
32
32
|
it "matches if the key is present" do
|
33
|
-
hash_including(:a).should
|
33
|
+
hash_including(:a).should eq({:a => 1, :b => 2})
|
34
34
|
end
|
35
35
|
|
36
36
|
it "matches if more keys are present" do
|
37
|
-
hash_including(:a, :b).should
|
37
|
+
hash_including(:a, :b).should eq({:a => 1, :b => 2, :c => 3})
|
38
38
|
end
|
39
39
|
|
40
40
|
it "matches a string against a given key" do
|
41
|
-
hash_including(:a).should
|
41
|
+
hash_including(:a).should eq({:a => "1", :b => 2})
|
42
42
|
end
|
43
43
|
|
44
44
|
it "matches if passed one key and one key/value pair" do
|
45
|
-
hash_including(:a, :b => 2).should
|
45
|
+
hash_including(:a, :b => 2).should eq({:a => 1, :b => 2})
|
46
46
|
end
|
47
47
|
|
48
48
|
it "matches if passed many keys and one key/value pair" do
|
49
|
-
hash_including(:a, :b, :c => 3).should
|
49
|
+
hash_including(:a, :b, :c => 3).should eq({:a => 1, :b => 2, :c => 3, :d => 4})
|
50
50
|
end
|
51
51
|
|
52
52
|
it "matches if passed many keys and many key/value pairs" do
|
53
|
-
hash_including(:a, :b, :c => 3, :e => 5).should
|
53
|
+
hash_including(:a, :b, :c => 3, :e => 5).should eq({:a => 1, :b => 2, :c => 3, :d => 4, :e => 5})
|
54
54
|
end
|
55
55
|
end
|
56
56
|
end
|
@@ -6,28 +6,28 @@ module RSpec
|
|
6
6
|
describe HashNotIncludingMatcher do
|
7
7
|
|
8
8
|
it "describes itself properly" do
|
9
|
-
HashNotIncludingMatcher.new(:a => 5).description.should
|
9
|
+
HashNotIncludingMatcher.new(:a => 5).description.should eq "hash_not_including(:a=>5)"
|
10
10
|
end
|
11
11
|
|
12
12
|
describe "passing" do
|
13
13
|
it "matches a hash without the specified key" do
|
14
|
-
hash_not_including(:c).should
|
14
|
+
hash_not_including(:c).should eq({:a => 1, :b => 2})
|
15
15
|
end
|
16
16
|
|
17
17
|
it "matches a hash with the specified key, but different value" do
|
18
|
-
hash_not_including(:b => 3).should
|
18
|
+
hash_not_including(:b => 3).should eq({:a => 1, :b => 2})
|
19
19
|
end
|
20
20
|
|
21
21
|
it "matches a hash without the specified key, given as anything()" do
|
22
|
-
hash_not_including(:c => anything).should
|
22
|
+
hash_not_including(:c => anything).should eq({:a => 1, :b => 2})
|
23
23
|
end
|
24
24
|
|
25
25
|
it "matches an empty hash" do
|
26
|
-
hash_not_including(:a).should
|
26
|
+
hash_not_including(:a).should eq({})
|
27
27
|
end
|
28
28
|
|
29
29
|
it "matches a hash without any of the specified keys" do
|
30
|
-
hash_not_including(:a, :b, :c).should
|
30
|
+
hash_not_including(:a, :b, :c).should eq({ :d => 7})
|
31
31
|
end
|
32
32
|
|
33
33
|
end
|
@@ -78,20 +78,20 @@ module RSpec
|
|
78
78
|
|
79
79
|
it "allows block to calculate return values" do
|
80
80
|
@mock.should_receive(:something).with("a","b","c").and_return { |a,b,c| c+b+a }
|
81
|
-
@mock.something("a","b","c").should
|
81
|
+
@mock.something("a","b","c").should eq "cba"
|
82
82
|
@mock.rspec_verify
|
83
83
|
end
|
84
84
|
|
85
85
|
it "allows parameter as return value" do
|
86
86
|
@mock.should_receive(:something).with("a","b","c").and_return("booh")
|
87
|
-
@mock.something("a","b","c").should
|
87
|
+
@mock.something("a","b","c").should eq "booh"
|
88
88
|
@mock.rspec_verify
|
89
89
|
end
|
90
90
|
|
91
91
|
it "returns the previously stubbed value if no return value is set" do
|
92
|
-
@mock.stub
|
92
|
+
@mock.stub(:something).with("a","b","c").and_return(:stubbed_value)
|
93
93
|
@mock.should_receive(:something).with("a","b","c")
|
94
|
-
@mock.something("a","b","c").should
|
94
|
+
@mock.something("a","b","c").should eq :stubbed_value
|
95
95
|
@mock.rspec_verify
|
96
96
|
end
|
97
97
|
|
@@ -137,7 +137,19 @@ module RSpec
|
|
137
137
|
@mock.rspec_verify
|
138
138
|
}.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"a\", \"b\", \"c\")\n got: (\"a\", \"d\", \"c\")")
|
139
139
|
end
|
140
|
-
|
140
|
+
|
141
|
+
describe 'with a method that has a default argument' do
|
142
|
+
it "raises an exception if the arguments don't match when the method is called, correctly reporting the offending arguments" do
|
143
|
+
def @mock.method_with_default_argument(arg={}); end
|
144
|
+
@mock.should_receive(:method_with_default_argument).with({})
|
145
|
+
|
146
|
+
expect {
|
147
|
+
@mock.method_with_default_argument(nil)
|
148
|
+
@mock.rspec_verify
|
149
|
+
}.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :method_with_default_argument with unexpected arguments\n expected: ({})\n got: (nil)")
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
141
153
|
it "fails if unexpected method called" do
|
142
154
|
lambda {
|
143
155
|
@mock.something("a","b","c")
|
@@ -147,11 +159,11 @@ module RSpec
|
|
147
159
|
|
148
160
|
it "uses block for expectation if provided" do
|
149
161
|
@mock.should_receive(:something) do | a, b |
|
150
|
-
a.should
|
151
|
-
b.should
|
162
|
+
a.should eq "a"
|
163
|
+
b.should eq "b"
|
152
164
|
"booh"
|
153
165
|
end
|
154
|
-
@mock.something("a", "b").should
|
166
|
+
@mock.something("a", "b").should eq "booh"
|
155
167
|
@mock.rspec_verify
|
156
168
|
end
|
157
169
|
|
@@ -169,8 +181,8 @@ module RSpec
|
|
169
181
|
eval("@mock.should_receive(:something) { |&block| a = block }")
|
170
182
|
b = lambda { }
|
171
183
|
@mock.something(&b)
|
172
|
-
a.should
|
173
|
-
@
|
184
|
+
a.should eq b
|
185
|
+
@mock.rspec_verify
|
174
186
|
end
|
175
187
|
|
176
188
|
it "fails right away when method defined as never is received" do
|
@@ -258,7 +270,7 @@ module RSpec
|
|
258
270
|
|
259
271
|
it "returns value from block by default" do
|
260
272
|
@mock.stub(:method_that_yields).and_yield
|
261
|
-
@mock.method_that_yields { :returned_obj }.should
|
273
|
+
@mock.method_that_yields { :returned_obj }.should eq :returned_obj
|
262
274
|
@mock.rspec_verify
|
263
275
|
end
|
264
276
|
|
@@ -266,17 +278,16 @@ module RSpec
|
|
266
278
|
@mock.should_receive(:yield_back).with(no_args()).once.and_yield
|
267
279
|
a = nil
|
268
280
|
@mock.yield_back {|*x| a = x}
|
269
|
-
a.should
|
281
|
+
a.should eq []
|
270
282
|
@mock.rspec_verify
|
271
283
|
end
|
272
284
|
|
273
285
|
it "yields 0 args multiple times to blocks that take a variable number of arguments" do
|
274
286
|
@mock.should_receive(:yield_back).once.with(no_args()).once.and_yield.
|
275
287
|
and_yield
|
276
|
-
a = nil
|
277
288
|
b = []
|
278
289
|
@mock.yield_back {|*a| b << a}
|
279
|
-
b.should
|
290
|
+
b.should eq [ [], [] ]
|
280
291
|
@mock.rspec_verify
|
281
292
|
end
|
282
293
|
|
@@ -284,7 +295,7 @@ module RSpec
|
|
284
295
|
@mock.should_receive(:yield_back).with(no_args()).once.and_yield(99)
|
285
296
|
a = nil
|
286
297
|
@mock.yield_back {|*x| a = x}
|
287
|
-
a.should
|
298
|
+
a.should eq [99]
|
288
299
|
@mock.rspec_verify
|
289
300
|
end
|
290
301
|
|
@@ -292,10 +303,9 @@ module RSpec
|
|
292
303
|
@mock.should_receive(:yield_back).once.with(no_args()).once.and_yield(99).
|
293
304
|
and_yield(43).
|
294
305
|
and_yield("something fruity")
|
295
|
-
a = nil
|
296
306
|
b = []
|
297
307
|
@mock.yield_back {|*a| b << a}
|
298
|
-
b.should
|
308
|
+
b.should eq [[99], [43], ["something fruity"]]
|
299
309
|
@mock.rspec_verify
|
300
310
|
end
|
301
311
|
|
@@ -303,7 +313,7 @@ module RSpec
|
|
303
313
|
@mock.should_receive(:yield_back).with(no_args()).once.and_yield(99, 27, "go")
|
304
314
|
a = nil
|
305
315
|
@mock.yield_back {|*x| a = x}
|
306
|
-
a.should
|
316
|
+
a.should eq [99, 27, "go"]
|
307
317
|
@mock.rspec_verify
|
308
318
|
end
|
309
319
|
|
@@ -311,10 +321,9 @@ module RSpec
|
|
311
321
|
@mock.should_receive(:yield_back).once.with(no_args()).once.and_yield(99, :green, "go").
|
312
322
|
and_yield("wait", :amber).
|
313
323
|
and_yield("stop", 12, :red)
|
314
|
-
a = nil
|
315
324
|
b = []
|
316
325
|
@mock.yield_back {|*a| b << a}
|
317
|
-
b.should
|
326
|
+
b.should eq [[99, :green, "go"], ["wait", :amber], ["stop", 12, :red]]
|
318
327
|
@mock.rspec_verify
|
319
328
|
end
|
320
329
|
|
@@ -322,7 +331,7 @@ module RSpec
|
|
322
331
|
@mock.should_receive(:yield_back).with(no_args()).once.and_yield(99)
|
323
332
|
a = nil
|
324
333
|
@mock.yield_back {|x| a = x}
|
325
|
-
a.should
|
334
|
+
a.should eq 99
|
326
335
|
@mock.rspec_verify
|
327
336
|
end
|
328
337
|
|
@@ -330,10 +339,9 @@ module RSpec
|
|
330
339
|
@mock.should_receive(:yield_back).once.with(no_args()).once.and_yield(99).
|
331
340
|
and_yield(43).
|
332
341
|
and_yield("something fruity")
|
333
|
-
a = nil
|
334
342
|
b = []
|
335
343
|
@mock.yield_back {|a| b << a}
|
336
|
-
b.should
|
344
|
+
b.should eq [99, 43, "something fruity"]
|
337
345
|
@mock.rspec_verify
|
338
346
|
end
|
339
347
|
|
@@ -341,8 +349,8 @@ module RSpec
|
|
341
349
|
@mock.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
|
342
350
|
a, b = nil
|
343
351
|
@mock.yield_back {|x,y| a=x; b=y}
|
344
|
-
a.should
|
345
|
-
b.should
|
352
|
+
a.should eq 'wha'
|
353
|
+
b.should eq 'zup'
|
346
354
|
@mock.rspec_verify
|
347
355
|
end
|
348
356
|
|
@@ -350,10 +358,9 @@ module RSpec
|
|
350
358
|
@mock.should_receive(:yield_back).once.with(no_args()).once.and_yield('wha', 'zup').
|
351
359
|
and_yield('not', 'down').
|
352
360
|
and_yield(14, 65)
|
353
|
-
a, b = nil
|
354
361
|
c = []
|
355
362
|
@mock.yield_back {|a,b| c << [a, b]}
|
356
|
-
c.should
|
363
|
+
c.should eq [['wha', 'zup'], ['not', 'down'], [14, 65]]
|
357
364
|
@mock.rspec_verify
|
358
365
|
end
|
359
366
|
|
@@ -369,7 +376,6 @@ module RSpec
|
|
369
376
|
and_yield('down').
|
370
377
|
and_yield(14, 65)
|
371
378
|
lambda {
|
372
|
-
a, b = nil
|
373
379
|
c = []
|
374
380
|
@mock.yield_back {|a,b| c << [a, b]}
|
375
381
|
}.should raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" yielded |\"down\"| to block with arity of 2")
|
@@ -496,8 +502,8 @@ module RSpec
|
|
496
502
|
it "does not mess with the stub's yielded values when also mocked" do
|
497
503
|
@mock.stub(:yield_back).and_yield(:stub_value)
|
498
504
|
@mock.should_receive(:yield_back).and_yield(:mock_value)
|
499
|
-
@mock.yield_back{|v| v.should
|
500
|
-
@mock.yield_back{|v| v.should
|
505
|
+
@mock.yield_back{|v| v.should eq :mock_value }
|
506
|
+
@mock.yield_back{|v| v.should eq :stub_value }
|
501
507
|
@mock.rspec_verify
|
502
508
|
end
|
503
509
|
|
@@ -506,14 +512,14 @@ module RSpec
|
|
506
512
|
File.should_receive(:open).and_yield(:first_call).and_yield(:second_call)
|
507
513
|
yielded_args = []
|
508
514
|
File.open {|v| yielded_args << v }
|
509
|
-
yielded_args.should
|
510
|
-
File.open {|v| v.should
|
515
|
+
yielded_args.should eq [:first_call, :second_call]
|
516
|
+
File.open {|v| v.should eq :stub_value }
|
511
517
|
File.rspec_verify
|
512
518
|
end
|
513
519
|
|
514
520
|
it "assigns stub return values" do
|
515
521
|
mock = RSpec::Mocks::Mock.new('name', :message => :response)
|
516
|
-
mock.message.should
|
522
|
+
mock.message.should eq :response
|
517
523
|
end
|
518
524
|
|
519
525
|
end
|
@@ -533,7 +539,7 @@ module RSpec
|
|
533
539
|
|
534
540
|
@mock.foo
|
535
541
|
|
536
|
-
@calls.should
|
542
|
+
@calls.should eq 1
|
537
543
|
end
|
538
544
|
|
539
545
|
it "calls the block after #should_receive after a similar stub" do
|
@@ -542,7 +548,7 @@ module RSpec
|
|
542
548
|
|
543
549
|
@mock.foo
|
544
550
|
|
545
|
-
@calls.should
|
551
|
+
@calls.should eq 1
|
546
552
|
end
|
547
553
|
|
548
554
|
it "calls the block after #once" do
|
@@ -550,7 +556,7 @@ module RSpec
|
|
550
556
|
|
551
557
|
@mock.foo
|
552
558
|
|
553
|
-
@calls.should
|
559
|
+
@calls.should eq 1
|
554
560
|
end
|
555
561
|
|
556
562
|
it "calls the block after #twice" do
|
@@ -559,7 +565,7 @@ module RSpec
|
|
559
565
|
@mock.foo
|
560
566
|
@mock.foo
|
561
567
|
|
562
|
-
@calls.should
|
568
|
+
@calls.should eq 2
|
563
569
|
end
|
564
570
|
|
565
571
|
it "calls the block after #times" do
|
@@ -567,7 +573,7 @@ module RSpec
|
|
567
573
|
|
568
574
|
(1..10).each { @mock.foo }
|
569
575
|
|
570
|
-
@calls.should
|
576
|
+
@calls.should eq 10
|
571
577
|
end
|
572
578
|
|
573
579
|
it "calls the block after #any_number_of_times" do
|
@@ -575,7 +581,7 @@ module RSpec
|
|
575
581
|
|
576
582
|
(1..7).each { @mock.foo }
|
577
583
|
|
578
|
-
@calls.should
|
584
|
+
@calls.should eq 7
|
579
585
|
end
|
580
586
|
|
581
587
|
it "calls the block after #ordered" do
|
@@ -585,7 +591,7 @@ module RSpec
|
|
585
591
|
@mock.foo
|
586
592
|
@mock.bar
|
587
593
|
|
588
|
-
@calls.should
|
594
|
+
@calls.should eq 2
|
589
595
|
end
|
590
596
|
end
|
591
597
|
|
@@ -600,7 +606,7 @@ module RSpec
|
|
600
606
|
describe "string representation generated by #to_str" do
|
601
607
|
it "looks the same as #to_s" do
|
602
608
|
double = double("Foo")
|
603
|
-
double.to_str.should
|
609
|
+
double.to_str.should eq double.to_s
|
604
610
|
end
|
605
611
|
end
|
606
612
|
|
@@ -612,8 +618,8 @@ module RSpec
|
|
612
618
|
|
613
619
|
it "does respond to initially stubbed methods" do
|
614
620
|
double = double(:foo => "woo", :bar => "car")
|
615
|
-
double.foo.should
|
616
|
-
double.bar.should
|
621
|
+
double.foo.should eq "woo"
|
622
|
+
double.bar.should eq "car"
|
617
623
|
end
|
618
624
|
end
|
619
625
|
|