rspec-mocks 2.12.1 → 2.12.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog.md +17 -0
- data/lib/rspec/mocks.rb +8 -0
- data/lib/rspec/mocks/message_expectation.rb +6 -11
- data/lib/rspec/mocks/method_double.rb +19 -2
- data/lib/rspec/mocks/mutate_const.rb +1 -1
- data/lib/rspec/mocks/proxy.rb +1 -1
- data/lib/rspec/mocks/version.rb +1 -1
- data/lib/spec/mocks.rb +4 -0
- data/spec/rspec/mocks/and_call_original_spec.rb +22 -0
- data/spec/rspec/mocks/and_yield_spec.rb +7 -7
- data/spec/rspec/mocks/any_instance/message_chains_spec.rb +6 -6
- data/spec/rspec/mocks/any_instance_spec.rb +94 -94
- data/spec/rspec/mocks/any_number_of_times_spec.rb +2 -2
- data/spec/rspec/mocks/argument_expectation_spec.rb +3 -3
- data/spec/rspec/mocks/at_least_spec.rb +15 -15
- data/spec/rspec/mocks/at_most_spec.rb +7 -7
- data/spec/rspec/mocks/block_return_value_spec.rb +6 -6
- data/spec/rspec/mocks/bug_report_10260_spec.rb +1 -1
- data/spec/rspec/mocks/bug_report_10263_spec.rb +6 -4
- data/spec/rspec/mocks/bug_report_11545_spec.rb +1 -1
- data/spec/rspec/mocks/bug_report_600_spec.rb +1 -1
- data/spec/rspec/mocks/bug_report_7611_spec.rb +1 -1
- data/spec/rspec/mocks/bug_report_8165_spec.rb +4 -4
- data/spec/rspec/mocks/bug_report_830_spec.rb +2 -2
- data/spec/rspec/mocks/bug_report_957_spec.rb +2 -2
- data/spec/rspec/mocks/configuration_spec.rb +4 -4
- data/spec/rspec/mocks/double_spec.rb +1 -1
- data/spec/rspec/mocks/failing_argument_matchers_spec.rb +1 -1
- data/spec/rspec/mocks/hash_excluding_matcher_spec.rb +13 -13
- data/spec/rspec/mocks/hash_including_matcher_spec.rb +30 -30
- data/spec/rspec/mocks/mock_ordering_spec.rb +8 -8
- data/spec/rspec/mocks/mock_space_spec.rb +4 -4
- data/spec/rspec/mocks/mock_spec.rb +94 -90
- data/spec/rspec/mocks/multiple_return_value_spec.rb +21 -21
- data/spec/rspec/mocks/mutate_const_spec.rb +112 -102
- data/spec/rspec/mocks/nil_expectation_warning_spec.rb +6 -12
- data/spec/rspec/mocks/null_object_mock_spec.rb +12 -12
- data/spec/rspec/mocks/once_counts_spec.rb +7 -7
- data/spec/rspec/mocks/options_hash_spec.rb +6 -6
- data/spec/rspec/mocks/partial_mock_spec.rb +15 -15
- data/spec/rspec/mocks/partial_mock_using_mocks_directly_spec.rb +17 -17
- data/spec/rspec/mocks/passing_argument_matchers_spec.rb +6 -6
- data/spec/rspec/mocks/precise_counts_spec.rb +7 -7
- data/spec/rspec/mocks/record_messages_spec.rb +4 -4
- data/spec/rspec/mocks/serialization_spec.rb +3 -3
- data/spec/rspec/mocks/stash_spec.rb +1 -1
- data/spec/rspec/mocks/stub_chain_spec.rb +22 -22
- data/spec/rspec/mocks/stub_implementation_spec.rb +10 -10
- data/spec/rspec/mocks/stub_spec.rb +50 -31
- data/spec/rspec/mocks/stubbed_message_expectations_spec.rb +3 -3
- data/spec/rspec/mocks/test_double_spec.rb +3 -3
- data/spec/rspec/mocks/to_ary_spec.rb +7 -7
- data/spec/rspec/mocks/twice_counts_spec.rb +8 -8
- data/spec/rspec/mocks_spec.rb +13 -4
- data/spec/spec_helper.rb +4 -0
- metadata +5 -5
data/Changelog.md
CHANGED
@@ -1,3 +1,18 @@
|
|
1
|
+
### 2.12.2 / 2013-01-27
|
2
|
+
[full changelog](http://github.com/rspec/rspec-mocks/compare/v2.12.1...v.2.12.2)
|
3
|
+
|
4
|
+
Bug fixes
|
5
|
+
|
6
|
+
* Fix `and_call_original` to work properly for methods defined
|
7
|
+
on a module extended onto an object instance (Myron Marston).
|
8
|
+
* Fix `stub_const` with an undefined constnat name to work properly
|
9
|
+
with constant strings that are prefixed with `::` -- and edge case
|
10
|
+
I missed in the bug fix in the 2.12.1 release (Myron Marston).
|
11
|
+
* Ensure method visibility on a partial mock is restored after reseting
|
12
|
+
method stubs, even on a singleton module (created via `extend self`)
|
13
|
+
when the method visibility differs between the instance and singleton
|
14
|
+
versions (Andy Lindeman).
|
15
|
+
|
1
16
|
### 2.12.1 / 2012-12-21
|
2
17
|
[full changelog](http://github.com/rspec/rspec-mocks/compare/v2.12.0...v2.12.1)
|
3
18
|
|
@@ -7,6 +22,8 @@ Bug fixes
|
|
7
22
|
(Myron Marston)
|
8
23
|
* Properly restore stubbed aliased methods on rubies
|
9
24
|
that report the incorrect owner (Myron Marston and Andy Lindeman).
|
25
|
+
* Fix `hide_const` and `stub_const` with a defined constnat name to
|
26
|
+
work properly with constant strings that are prefixed with `::` (Myron Marston).
|
10
27
|
|
11
28
|
### 2.12.0 / 2012-11-12
|
12
29
|
[full changelog](http://github.com/rspec/rspec-mocks/compare/v2.11.3...v2.12.0)
|
data/lib/rspec/mocks.rb
CHANGED
@@ -27,6 +27,14 @@ module RSpec
|
|
27
27
|
@configuration ||= Configuration.new
|
28
28
|
end
|
29
29
|
|
30
|
+
# @api private
|
31
|
+
# Used internally by RSpec to display custom deprecation warnings. This
|
32
|
+
# is also defined in rspec-core, but we can't assume it's loaded since
|
33
|
+
# rspec-expectations should be usable w/o rspec-core.
|
34
|
+
def warn_deprecation(message)
|
35
|
+
warn(message)
|
36
|
+
end
|
37
|
+
|
30
38
|
private
|
31
39
|
|
32
40
|
def add_extensions
|
@@ -20,8 +20,6 @@ module RSpec
|
|
20
20
|
@expected_received_count = expected_received_count
|
21
21
|
@argument_list_matcher = ArgumentListMatcher.new(ArgumentMatchers::AnyArgsMatcher.new)
|
22
22
|
@consecutive = false
|
23
|
-
@exception_to_raise = nil
|
24
|
-
@args_to_throw = []
|
25
23
|
@order_group = expectation_ordering
|
26
24
|
@at_least = @at_most = @exactly = nil
|
27
25
|
@args_to_yield = []
|
@@ -141,10 +139,10 @@ module RSpec
|
|
141
139
|
# car.stub(:go).and_raise(OutOfGas.new(2, :oz))
|
142
140
|
def and_raise(exception = RuntimeError, message = nil)
|
143
141
|
if exception.respond_to?(:exception)
|
144
|
-
|
145
|
-
else
|
146
|
-
@exception_to_raise = exception
|
142
|
+
exception = message ? exception.exception(message) : exception.exception
|
147
143
|
end
|
144
|
+
|
145
|
+
self.implementation = Proc.new { raise exception }
|
148
146
|
end
|
149
147
|
|
150
148
|
# @overload and_throw(symbol)
|
@@ -157,8 +155,8 @@ module RSpec
|
|
157
155
|
#
|
158
156
|
# car.stub(:go).and_throw(:out_of_gas)
|
159
157
|
# car.stub(:go).and_throw(:out_of_gas, :level => 0.1)
|
160
|
-
def and_throw(
|
161
|
-
|
158
|
+
def and_throw(*args)
|
159
|
+
self.implementation = Proc.new { throw *args }
|
162
160
|
end
|
163
161
|
|
164
162
|
# Tells the object to yield one or more args to a block when the message
|
@@ -195,9 +193,6 @@ module RSpec
|
|
195
193
|
@order_group.handle_order_constraint self
|
196
194
|
|
197
195
|
begin
|
198
|
-
raise(@exception_to_raise) unless @exception_to_raise.nil?
|
199
|
-
Kernel::throw(*@args_to_throw) unless @args_to_throw.empty?
|
200
|
-
|
201
196
|
default_return_val = call_with_yield(&block) if !@args_to_yield.empty? || @eval_context
|
202
197
|
|
203
198
|
if @consecutive
|
@@ -493,7 +488,7 @@ module RSpec
|
|
493
488
|
def and_return(*)
|
494
489
|
# no-op
|
495
490
|
# @deprecated and_return is not supported with negative message expectations.
|
496
|
-
RSpec.warn_deprecation <<-MSG
|
491
|
+
RSpec::Mocks.warn_deprecation <<-MSG
|
497
492
|
|
498
493
|
DEPRECATION: `and_return` with `should_not_receive` is deprecated. Called from #{caller(0)[1]}
|
499
494
|
MSG
|
@@ -56,8 +56,8 @@ module RSpec
|
|
56
56
|
meth
|
57
57
|
else
|
58
58
|
begin
|
59
|
-
# Example: an instance method defined on @object's
|
60
|
-
|
59
|
+
# Example: an instance method defined on one of @object's ancestors.
|
60
|
+
original_method_from_ancestor(object_singleton_class.ancestors)
|
61
61
|
rescue NameError
|
62
62
|
raise unless @object.respond_to?(:superclass)
|
63
63
|
|
@@ -97,6 +97,14 @@ module RSpec
|
|
97
97
|
any_instance_class_recorder_observing_method?(superklass)
|
98
98
|
end
|
99
99
|
|
100
|
+
def original_method_from_ancestor(ancestors)
|
101
|
+
klass, *rest = ancestors
|
102
|
+
klass.instance_method(@method_name).bind(@object)
|
103
|
+
rescue NameError
|
104
|
+
raise if rest.empty?
|
105
|
+
original_method_from_ancestor(rest)
|
106
|
+
end
|
107
|
+
|
100
108
|
if RUBY_VERSION.to_f > 1.8
|
101
109
|
# @private
|
102
110
|
def original_method_from_superclass
|
@@ -145,6 +153,7 @@ module RSpec
|
|
145
153
|
def configure_method
|
146
154
|
RSpec::Mocks::space.add(@object) if RSpec::Mocks::space
|
147
155
|
warn_if_nil_class
|
156
|
+
@original_visibility = visibility_for_method
|
148
157
|
@method_stasher.stash unless @method_is_proxied
|
149
158
|
define_proxy_method
|
150
159
|
end
|
@@ -173,9 +182,17 @@ module RSpec
|
|
173
182
|
|
174
183
|
object_singleton_class.__send__(:remove_method, @method_name)
|
175
184
|
@method_stasher.restore
|
185
|
+
restore_original_visibility
|
186
|
+
|
176
187
|
@method_is_proxied = false
|
177
188
|
end
|
178
189
|
|
190
|
+
# @private
|
191
|
+
def restore_original_visibility
|
192
|
+
return unless object_singleton_class.method_defined?(@method_name) || object_singleton_class.private_method_defined?(@method_name)
|
193
|
+
object_singleton_class.class_eval(@original_visibility, __FILE__, __LINE__)
|
194
|
+
end
|
195
|
+
|
179
196
|
# @private
|
180
197
|
def verify
|
181
198
|
expectations.each {|e| e.verify_messages_received}
|
@@ -199,7 +199,7 @@ module RSpec
|
|
199
199
|
attr_reader :original_value, :full_constant_name
|
200
200
|
|
201
201
|
def initialize(full_constant_name, mutated_value, transfer_nested_constants)
|
202
|
-
@full_constant_name = full_constant_name
|
202
|
+
@full_constant_name = normalize_const_name(full_constant_name)
|
203
203
|
@mutated_value = mutated_value
|
204
204
|
@transfer_nested_constants = transfer_nested_constants
|
205
205
|
@context_parts = @full_constant_name.split('::')
|
data/lib/rspec/mocks/proxy.rb
CHANGED
data/lib/rspec/mocks/version.rb
CHANGED
data/lib/spec/mocks.rb
CHANGED
@@ -37,6 +37,28 @@ describe "and_call_original" do
|
|
37
37
|
expect(instance.foo).to eq(:bar)
|
38
38
|
end
|
39
39
|
|
40
|
+
it 'works for methods added through an extended module' do
|
41
|
+
instance.extend Module.new { def foo; :bar; end }
|
42
|
+
instance.should_receive(:foo).and_call_original
|
43
|
+
expect(instance.foo).to eq(:bar)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "works for method added through an extended module onto a class's ancestor" do
|
47
|
+
sub_sub_klass = Class.new(Class.new(klass))
|
48
|
+
klass.extend Module.new { def foo; :bar; end }
|
49
|
+
sub_sub_klass.should_receive(:foo).and_call_original
|
50
|
+
expect(sub_sub_klass.foo).to eq(:bar)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "finds the method on the most direct ancestor even if the method " +
|
54
|
+
"is available on more distant ancestors" do
|
55
|
+
klass.extend Module.new { def foo; :klass_bar; end }
|
56
|
+
sub_klass = Class.new(klass)
|
57
|
+
sub_klass.extend Module.new { def foo; :sub_klass_bar; end }
|
58
|
+
sub_klass.should_receive(:foo).and_call_original
|
59
|
+
expect(sub_klass.foo).to eq(:sub_klass_bar)
|
60
|
+
end
|
61
|
+
|
40
62
|
context 'when using any_instance' do
|
41
63
|
it 'works for instance methods defined on the class' do
|
42
64
|
klass.any_instance.should_receive(:meth_1).and_call_original
|
@@ -6,18 +6,18 @@ describe RSpec::Mocks::Mock do
|
|
6
6
|
|
7
7
|
describe "#and_yield" do
|
8
8
|
context "with eval context as block argument" do
|
9
|
-
|
9
|
+
|
10
10
|
it "evaluates the supplied block as it is read" do
|
11
11
|
evaluated = false
|
12
12
|
obj.stub(:method_that_accepts_a_block).and_yield do |eval_context|
|
13
13
|
evaluated = true
|
14
14
|
end
|
15
|
-
evaluated.
|
15
|
+
expect(evaluated).to be_true
|
16
16
|
end
|
17
17
|
|
18
18
|
it "passes an eval context object to the supplied block" do
|
19
19
|
obj.stub(:method_that_accepts_a_block).and_yield do |eval_context|
|
20
|
-
eval_context.
|
20
|
+
expect(eval_context).not_to be_nil
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -33,7 +33,7 @@ describe RSpec::Mocks::Mock do
|
|
33
33
|
actual_eval_context = self
|
34
34
|
end
|
35
35
|
|
36
|
-
actual_eval_context.
|
36
|
+
expect(actual_eval_context).to equal(expected_eval_context)
|
37
37
|
end
|
38
38
|
|
39
39
|
context "and no yielded arguments" do
|
@@ -63,7 +63,7 @@ describe RSpec::Mocks::Mock do
|
|
63
63
|
# foo is not called here
|
64
64
|
end
|
65
65
|
|
66
|
-
|
66
|
+
expect { configured_eval_context.rspec_verify }.to raise_error(RSpec::Mocks::MockExpectationError)
|
67
67
|
end
|
68
68
|
|
69
69
|
end
|
@@ -102,8 +102,8 @@ describe RSpec::Mocks::Mock do
|
|
102
102
|
# foo is not called here
|
103
103
|
end
|
104
104
|
|
105
|
-
|
106
|
-
|
105
|
+
expect { configured_eval_context.rspec_verify }.to raise_error(RSpec::Mocks::MockExpectationError)
|
106
|
+
expect { yielded_arg.rspec_verify }.to raise_error(RSpec::Mocks::MockExpectationError)
|
107
107
|
end
|
108
108
|
|
109
109
|
end
|
@@ -7,13 +7,13 @@ describe RSpec::Mocks::AnyInstance::MessageChains do
|
|
7
7
|
|
8
8
|
it "knows if a method does not have an expectation set on it" do
|
9
9
|
chains.add(:method_name, stub_chain)
|
10
|
-
chains.has_expectation?(:method_name).
|
10
|
+
expect(chains.has_expectation?(:method_name)).to be_false
|
11
11
|
end
|
12
12
|
|
13
13
|
it "knows if a method has an expectation set on it" do
|
14
14
|
chains.add(:method_name, stub_chain)
|
15
15
|
chains.add(:method_name, expectation_chain)
|
16
|
-
chains.has_expectation?(:method_name).
|
16
|
+
expect(chains.has_expectation?(:method_name)).to be_true
|
17
17
|
end
|
18
18
|
|
19
19
|
it "can remove all stub chains" do
|
@@ -22,19 +22,19 @@ describe RSpec::Mocks::AnyInstance::MessageChains do
|
|
22
22
|
chains.add(:method_name, RSpec::Mocks::AnyInstance::StubChain.new)
|
23
23
|
|
24
24
|
chains.remove_stub_chains_for!(:method_name)
|
25
|
-
chains[:method_name].
|
25
|
+
expect(chains[:method_name]).to eq([expectation_chain])
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
context "creating stub chains" do
|
29
29
|
it "understands how to add a stub chain for a method" do
|
30
30
|
chains.add(:method_name, stub_chain)
|
31
|
-
chains[:method_name].
|
31
|
+
expect(chains[:method_name]).to eq([stub_chain])
|
32
32
|
end
|
33
33
|
|
34
34
|
it "allows multiple stub chains for a method" do
|
35
35
|
chains.add(:method_name, stub_chain)
|
36
36
|
chains.add(:method_name, another_stub_chain = RSpec::Mocks::AnyInstance::StubChain.new)
|
37
|
-
chains[:method_name].
|
37
|
+
expect(chains[:method_name]).to eq([stub_chain, another_stub_chain])
|
38
38
|
end
|
39
39
|
end
|
40
40
|
end
|
@@ -19,41 +19,41 @@ module RSpec
|
|
19
19
|
context "invocation order" do
|
20
20
|
context "#stub" do
|
21
21
|
it "raises an error if 'stub' follows 'with'" do
|
22
|
-
|
22
|
+
expect { klass.any_instance.with("1").stub(:foo) }.to raise_error(NoMethodError)
|
23
23
|
end
|
24
24
|
|
25
25
|
it "raises an error if 'with' follows 'and_return'" do
|
26
|
-
|
26
|
+
expect { klass.any_instance.stub(:foo).and_return(1).with("1") }.to raise_error(NoMethodError)
|
27
27
|
end
|
28
28
|
|
29
29
|
it "raises an error if 'with' follows 'and_raise'" do
|
30
|
-
|
30
|
+
expect { klass.any_instance.stub(:foo).and_raise(1).with("1") }.to raise_error(NoMethodError)
|
31
31
|
end
|
32
32
|
|
33
33
|
it "raises an error if 'with' follows 'and_yield'" do
|
34
|
-
|
34
|
+
expect { klass.any_instance.stub(:foo).and_yield(1).with("1") }.to raise_error(NoMethodError)
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
38
|
context "#stub_chain" do
|
39
39
|
it "raises an error if 'stub_chain' follows 'any_instance'" do
|
40
|
-
|
40
|
+
expect { klass.any_instance.and_return("1").stub_chain(:foo, :bar) }.to raise_error(NoMethodError)
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
44
|
context "#should_receive" do
|
45
45
|
it "raises an error if 'should_receive' follows 'with'" do
|
46
|
-
|
46
|
+
expect { klass.any_instance.with("1").should_receive(:foo) }.to raise_error(NoMethodError)
|
47
47
|
end
|
48
48
|
|
49
49
|
it "raises an error if 'with' follows 'and_return'" do
|
50
50
|
pending "see Github issue #42"
|
51
|
-
|
51
|
+
expect { klass.any_instance.should_receive(:foo).and_return(1).with("1") }.to raise_error(NoMethodError)
|
52
52
|
end
|
53
53
|
|
54
54
|
it "raises an error if 'with' follows 'and_raise'" do
|
55
55
|
pending "see Github issue #42"
|
56
|
-
|
56
|
+
expect { klass.any_instance.should_receive(:foo).and_raise(1).with("1") }.to raise_error(NoMethodError)
|
57
57
|
end
|
58
58
|
end
|
59
59
|
end
|
@@ -61,36 +61,36 @@ module RSpec
|
|
61
61
|
context "with #stub" do
|
62
62
|
it "does not suppress an exception when a method that doesn't exist is invoked" do
|
63
63
|
klass.any_instance.stub(:foo)
|
64
|
-
|
64
|
+
expect { klass.new.bar }.to raise_error(NoMethodError)
|
65
65
|
end
|
66
66
|
|
67
67
|
context 'multiple methods' do
|
68
68
|
it "allows multiple methods to be stubbed in a single invocation" do
|
69
69
|
klass.any_instance.stub(:foo => 'foo', :bar => 'bar')
|
70
70
|
instance = klass.new
|
71
|
-
instance.foo.
|
72
|
-
instance.bar.
|
71
|
+
expect(instance.foo).to eq('foo')
|
72
|
+
expect(instance.bar).to eq('bar')
|
73
73
|
end
|
74
74
|
|
75
75
|
it "adheres to the contract of multiple method stubbing withou any instance" do
|
76
|
-
Object.new.stub(:foo => 'foo', :bar => 'bar').
|
77
|
-
klass.any_instance.stub(:foo => 'foo', :bar => 'bar').
|
76
|
+
expect(Object.new.stub(:foo => 'foo', :bar => 'bar')).to eq(:foo => 'foo', :bar => 'bar')
|
77
|
+
expect(klass.any_instance.stub(:foo => 'foo', :bar => 'bar')).to eq(:foo => 'foo', :bar => 'bar')
|
78
78
|
end
|
79
79
|
|
80
80
|
context "allows a chain of methods to be stubbed using #stub_chain" do
|
81
81
|
it "given symbols representing the methods" do
|
82
82
|
klass.any_instance.stub_chain(:one, :two, :three).and_return(:four)
|
83
|
-
klass.new.one.two.three.
|
83
|
+
expect(klass.new.one.two.three).to eq(:four)
|
84
84
|
end
|
85
85
|
|
86
86
|
it "given a hash as the last argument uses the value as the expected return value" do
|
87
87
|
klass.any_instance.stub_chain(:one, :two, :three => :four)
|
88
|
-
klass.new.one.two.three.
|
88
|
+
expect(klass.new.one.two.three).to eq(:four)
|
89
89
|
end
|
90
90
|
|
91
91
|
it "given a string of '.' separated method names representing the chain" do
|
92
92
|
klass.any_instance.stub_chain('one.two.three').and_return(:four)
|
93
|
-
klass.new.one.two.three.
|
93
|
+
expect(klass.new.one.two.three).to eq(:four)
|
94
94
|
end
|
95
95
|
end
|
96
96
|
end
|
@@ -98,14 +98,14 @@ module RSpec
|
|
98
98
|
context "behaves as 'every instance'" do
|
99
99
|
it "stubs every instance in the spec" do
|
100
100
|
klass.any_instance.stub(:foo).and_return(result = Object.new)
|
101
|
-
klass.new.foo.
|
102
|
-
klass.new.foo.
|
101
|
+
expect(klass.new.foo).to eq(result)
|
102
|
+
expect(klass.new.foo).to eq(result)
|
103
103
|
end
|
104
104
|
|
105
105
|
it "stubs instance created before any_instance was called" do
|
106
106
|
instance = klass.new
|
107
107
|
klass.any_instance.stub(:foo).and_return(result = Object.new)
|
108
|
-
instance.foo.
|
108
|
+
expect(instance.foo).to eq(result)
|
109
109
|
end
|
110
110
|
end
|
111
111
|
|
@@ -117,8 +117,8 @@ module RSpec
|
|
117
117
|
|
118
118
|
it "returns the stubbed value when arguments match" do
|
119
119
|
instance = klass.new
|
120
|
-
instance.foo(:param_one, :param_two).
|
121
|
-
instance.foo(:param_three, :param_four).
|
120
|
+
expect(instance.foo(:param_one, :param_two)).to eq(:result_one)
|
121
|
+
expect(instance.foo(:param_three, :param_four)).to eq(:result_two)
|
122
122
|
end
|
123
123
|
|
124
124
|
it "fails the spec with an expectation error when the arguments do not match" do
|
@@ -136,32 +136,32 @@ module RSpec
|
|
136
136
|
|
137
137
|
it "stubs a method" do
|
138
138
|
instance = klass.new
|
139
|
-
instance.foo.
|
140
|
-
instance.bar.
|
139
|
+
expect(instance.foo).to eq(1)
|
140
|
+
expect(instance.bar).to eq(2)
|
141
141
|
end
|
142
142
|
|
143
143
|
it "returns the same value for calls on different instances" do
|
144
|
-
klass.new.foo.
|
145
|
-
klass.new.bar.
|
144
|
+
expect(klass.new.foo).to eq(klass.new.foo)
|
145
|
+
expect(klass.new.bar).to eq(klass.new.bar)
|
146
146
|
end
|
147
147
|
end
|
148
148
|
|
149
149
|
context "with #and_return" do
|
150
150
|
it "stubs a method that doesn't exist" do
|
151
151
|
klass.any_instance.stub(:foo).and_return(1)
|
152
|
-
klass.new.foo.
|
152
|
+
expect(klass.new.foo).to eq(1)
|
153
153
|
end
|
154
154
|
|
155
155
|
it "stubs a method that exists" do
|
156
156
|
klass.any_instance.stub(:existing_method).and_return(1)
|
157
|
-
klass.new.existing_method.
|
157
|
+
expect(klass.new.existing_method).to eq(1)
|
158
158
|
end
|
159
159
|
|
160
160
|
it "returns the same object for calls on different instances" do
|
161
161
|
return_value = Object.new
|
162
162
|
klass.any_instance.stub(:foo).and_return(return_value)
|
163
|
-
klass.new.foo.
|
164
|
-
klass.new.foo.
|
163
|
+
expect(klass.new.foo).to be(return_value)
|
164
|
+
expect(klass.new.foo).to be(return_value)
|
165
165
|
end
|
166
166
|
end
|
167
167
|
|
@@ -169,70 +169,70 @@ module RSpec
|
|
169
169
|
it "yields the value specified" do
|
170
170
|
yielded_value = Object.new
|
171
171
|
klass.any_instance.stub(:foo).and_yield(yielded_value)
|
172
|
-
klass.new.foo{|value| value.
|
172
|
+
klass.new.foo{|value| expect(value).to be(yielded_value)}
|
173
173
|
end
|
174
174
|
end
|
175
175
|
|
176
176
|
context "with #and_raise" do
|
177
177
|
it "stubs a method that doesn't exist" do
|
178
178
|
klass.any_instance.stub(:foo).and_raise(CustomErrorForAnyInstanceSpec)
|
179
|
-
|
179
|
+
expect { klass.new.foo}.to raise_error(CustomErrorForAnyInstanceSpec)
|
180
180
|
end
|
181
181
|
|
182
182
|
it "stubs a method that exists" do
|
183
183
|
klass.any_instance.stub(:existing_method).and_raise(CustomErrorForAnyInstanceSpec)
|
184
|
-
|
184
|
+
expect { klass.new.existing_method}.to raise_error(CustomErrorForAnyInstanceSpec)
|
185
185
|
end
|
186
186
|
end
|
187
187
|
|
188
188
|
context "with a block" do
|
189
189
|
it "stubs a method" do
|
190
190
|
klass.any_instance.stub(:foo) { 1 }
|
191
|
-
klass.new.foo.
|
191
|
+
expect(klass.new.foo).to eq(1)
|
192
192
|
end
|
193
193
|
|
194
194
|
it "returns the same computed value for calls on different instances" do
|
195
195
|
klass.any_instance.stub(:foo) { 1 + 2 }
|
196
|
-
klass.new.foo.
|
196
|
+
expect(klass.new.foo).to eq(klass.new.foo)
|
197
197
|
end
|
198
198
|
end
|
199
199
|
|
200
200
|
context "core ruby objects" do
|
201
201
|
it "works uniformly across *everything*" do
|
202
202
|
Object.any_instance.stub(:foo).and_return(1)
|
203
|
-
Object.new.foo.
|
203
|
+
expect(Object.new.foo).to eq(1)
|
204
204
|
end
|
205
205
|
|
206
206
|
it "works with the non-standard constructor []" do
|
207
207
|
Array.any_instance.stub(:foo).and_return(1)
|
208
|
-
[].foo.
|
208
|
+
expect([].foo).to eq(1)
|
209
209
|
end
|
210
210
|
|
211
211
|
it "works with the non-standard constructor {}" do
|
212
212
|
Hash.any_instance.stub(:foo).and_return(1)
|
213
|
-
{}.foo.
|
213
|
+
expect({}.foo).to eq(1)
|
214
214
|
end
|
215
215
|
|
216
216
|
it "works with the non-standard constructor \"\"" do
|
217
217
|
String.any_instance.stub(:foo).and_return(1)
|
218
|
-
"".foo.
|
218
|
+
expect("".foo).to eq(1)
|
219
219
|
end
|
220
220
|
|
221
221
|
it "works with the non-standard constructor \'\'" do
|
222
222
|
String.any_instance.stub(:foo).and_return(1)
|
223
|
-
''.foo.
|
223
|
+
expect(''.foo).to eq(1)
|
224
224
|
end
|
225
225
|
|
226
226
|
it "works with the non-standard constructor module" do
|
227
227
|
Module.any_instance.stub(:foo).and_return(1)
|
228
228
|
module RSpec::SampleRspecTestModule;end
|
229
|
-
RSpec::SampleRspecTestModule.foo.
|
229
|
+
expect(RSpec::SampleRspecTestModule.foo).to eq(1)
|
230
230
|
end
|
231
231
|
|
232
232
|
it "works with the non-standard constructor class" do
|
233
233
|
Class.any_instance.stub(:foo).and_return(1)
|
234
234
|
class RSpec::SampleRspecTestClass;end
|
235
|
-
RSpec::SampleRspecTestClass.foo.
|
235
|
+
expect(RSpec::SampleRspecTestClass.foo).to eq(1)
|
236
236
|
end
|
237
237
|
end
|
238
238
|
end
|
@@ -249,14 +249,14 @@ module RSpec
|
|
249
249
|
it "replaces the stubbed method with the original method" do
|
250
250
|
klass.any_instance.stub(:existing_method)
|
251
251
|
klass.any_instance.unstub(:existing_method)
|
252
|
-
klass.new.existing_method.
|
252
|
+
expect(klass.new.existing_method).to eq(:existing_method_return_value)
|
253
253
|
end
|
254
254
|
|
255
255
|
it "removes all stubs with the supplied method name" do
|
256
256
|
klass.any_instance.stub(:existing_method).with(1)
|
257
257
|
klass.any_instance.stub(:existing_method).with(2)
|
258
258
|
klass.any_instance.unstub(:existing_method)
|
259
|
-
klass.new.existing_method.
|
259
|
+
expect(klass.new.existing_method).to eq(:existing_method_return_value)
|
260
260
|
end
|
261
261
|
|
262
262
|
it "does not remove any expectations with the same method name" do
|
@@ -264,42 +264,42 @@ module RSpec
|
|
264
264
|
klass.any_instance.stub(:existing_method_with_arguments).with(1)
|
265
265
|
klass.any_instance.stub(:existing_method_with_arguments).with(2)
|
266
266
|
klass.any_instance.unstub(:existing_method_with_arguments)
|
267
|
-
klass.new.existing_method_with_arguments(3).
|
267
|
+
expect(klass.new.existing_method_with_arguments(3)).to eq(:three)
|
268
268
|
end
|
269
269
|
|
270
270
|
it "raises a MockExpectationError if the method has not been stubbed" do
|
271
|
-
|
271
|
+
expect {
|
272
272
|
klass.any_instance.unstub(:existing_method)
|
273
|
-
|
273
|
+
}.to raise_error(RSpec::Mocks::MockExpectationError, 'The method `existing_method` was not stubbed or was already unstubbed')
|
274
274
|
end
|
275
275
|
end
|
276
276
|
|
277
277
|
context "with #should_not_receive" do
|
278
278
|
it "fails if the method is called" do
|
279
279
|
klass.any_instance.should_not_receive(:existing_method)
|
280
|
-
|
280
|
+
expect { klass.new.existing_method }.to raise_error(RSpec::Mocks::MockExpectationError)
|
281
281
|
end
|
282
282
|
|
283
283
|
it "passes if no method is called" do
|
284
|
-
|
284
|
+
expect { klass.any_instance.should_not_receive(:existing_method) }.to_not raise_error
|
285
285
|
end
|
286
286
|
|
287
287
|
it "passes if only a different method is called" do
|
288
288
|
klass.any_instance.should_not_receive(:existing_method)
|
289
|
-
|
289
|
+
expect { klass.new.another_existing_method }.to_not raise_error
|
290
290
|
end
|
291
291
|
|
292
292
|
context "with constraints" do
|
293
293
|
it "fails if the method is called with the specified parameters" do
|
294
294
|
klass.any_instance.should_not_receive(:existing_method_with_arguments).with(:argument_one, :argument_two)
|
295
|
-
|
295
|
+
expect {
|
296
296
|
klass.new.existing_method_with_arguments(:argument_one, :argument_two)
|
297
|
-
|
297
|
+
}.to raise_error(RSpec::Mocks::MockExpectationError)
|
298
298
|
end
|
299
299
|
|
300
300
|
it "passes if the method is called with different parameters" do
|
301
301
|
klass.any_instance.should_not_receive(:existing_method_with_arguments).with(:argument_one, :argument_two)
|
302
|
-
|
302
|
+
expect { klass.new.existing_method_with_arguments(:argument_three, :argument_four) }.to_not raise_error
|
303
303
|
end
|
304
304
|
end
|
305
305
|
end
|
@@ -311,7 +311,7 @@ module RSpec
|
|
311
311
|
context "with an expectation is set on a method which does not exist" do
|
312
312
|
it "returns the expected value" do
|
313
313
|
klass.any_instance.should_receive(:foo).and_return(1)
|
314
|
-
klass.new.foo(1).
|
314
|
+
expect(klass.new.foo(1)).to eq(1)
|
315
315
|
end
|
316
316
|
|
317
317
|
it "fails if an instance is created but no invocation occurs" do
|
@@ -343,7 +343,7 @@ module RSpec
|
|
343
343
|
|
344
344
|
instance = klass.new
|
345
345
|
instance.should_receive(:foo).and_return(result = Object.new)
|
346
|
-
instance.foo.
|
346
|
+
expect(instance.foo).to eq(result)
|
347
347
|
end
|
348
348
|
|
349
349
|
context "behaves as 'exactly one instance'" do
|
@@ -373,7 +373,7 @@ module RSpec
|
|
373
373
|
klass.new.foo
|
374
374
|
klass.rspec_verify
|
375
375
|
end.to(raise_error(RSpec::Mocks::MockExpectationError) do |error|
|
376
|
-
error.message.
|
376
|
+
expect(error.message).not_to eq(existing_method_expectation_error_message)
|
377
377
|
end)
|
378
378
|
end
|
379
379
|
|
@@ -382,7 +382,7 @@ module RSpec
|
|
382
382
|
klass.any_instance.should_receive(:foo)
|
383
383
|
klass.should_receive(:woot).and_return(result = Object.new)
|
384
384
|
klass.new.foo
|
385
|
-
klass.woot.
|
385
|
+
expect(klass.woot).to eq(result)
|
386
386
|
end
|
387
387
|
end
|
388
388
|
end
|
@@ -390,7 +390,7 @@ module RSpec
|
|
390
390
|
context "with an expectation is set on a method that exists" do
|
391
391
|
it "returns the expected value" do
|
392
392
|
klass.any_instance.should_receive(:existing_method).and_return(1)
|
393
|
-
klass.new.existing_method(1).
|
393
|
+
expect(klass.new.existing_method(1)).to eq(1)
|
394
394
|
end
|
395
395
|
|
396
396
|
it "fails if an instance is created but no invocation occurs" do
|
@@ -444,15 +444,15 @@ module RSpec
|
|
444
444
|
|
445
445
|
it "returns the expected value when arguments match" do
|
446
446
|
instance = klass.new
|
447
|
-
instance.foo(:param_one, :param_two).
|
448
|
-
instance.foo(:param_three, :param_four).
|
447
|
+
expect(instance.foo(:param_one, :param_two)).to eq(:result_one)
|
448
|
+
expect(instance.foo(:param_three, :param_four)).to eq(:result_two)
|
449
449
|
end
|
450
450
|
|
451
451
|
it "fails when the arguments match but different instances are used" do
|
452
452
|
instances = Array.new(2) { klass.new }
|
453
453
|
expect do
|
454
|
-
instances[0].foo(:param_one, :param_two).
|
455
|
-
instances[1].foo(:param_three, :param_four).
|
454
|
+
expect(instances[0].foo(:param_one, :param_two)).to eq(:result_one)
|
455
|
+
expect(instances[1].foo(:param_three, :param_four)).to eq(:result_two)
|
456
456
|
end.to raise_error(RSpec::Mocks::MockExpectationError)
|
457
457
|
|
458
458
|
# ignore the fact that should_receive expectations were not met
|
@@ -460,10 +460,10 @@ module RSpec
|
|
460
460
|
end
|
461
461
|
|
462
462
|
it "is not affected by the invocation of existing methods on other instances" do
|
463
|
-
klass.new.existing_method_with_arguments(:param_one, :param_two).
|
463
|
+
expect(klass.new.existing_method_with_arguments(:param_one, :param_two)).to eq(:existing_method_with_arguments_return_value)
|
464
464
|
instance = klass.new
|
465
|
-
instance.foo(:param_one, :param_two).
|
466
|
-
instance.foo(:param_three, :param_four).
|
465
|
+
expect(instance.foo(:param_one, :param_two)).to eq(:result_one)
|
466
|
+
expect(instance.foo(:param_three, :param_four)).to eq(:result_two)
|
467
467
|
end
|
468
468
|
|
469
469
|
it "fails when arguments do not match" do
|
@@ -615,7 +615,7 @@ module RSpec
|
|
615
615
|
it "passes when the other expecations are met" do
|
616
616
|
klass.any_instance.should_receive(:foo).never
|
617
617
|
klass.any_instance.should_receive(:existing_method).and_return(5)
|
618
|
-
klass.new.existing_method.
|
618
|
+
expect(klass.new.existing_method).to eq(5)
|
619
619
|
end
|
620
620
|
|
621
621
|
it "fails when the other expecations are not met" do
|
@@ -644,14 +644,14 @@ module RSpec
|
|
644
644
|
it "does not interfere with other expectations" do
|
645
645
|
klass.any_instance.should_receive(:foo).any_number_of_times
|
646
646
|
klass.any_instance.should_receive(:existing_method).and_return(5)
|
647
|
-
klass.new.existing_method.
|
647
|
+
expect(klass.new.existing_method).to eq(5)
|
648
648
|
end
|
649
649
|
|
650
650
|
context "when combined with other expectations" do
|
651
651
|
it "passes when the other expecations are met" do
|
652
652
|
klass.any_instance.should_receive(:foo).any_number_of_times
|
653
653
|
klass.any_instance.should_receive(:existing_method).and_return(5)
|
654
|
-
klass.new.existing_method.
|
654
|
+
expect(klass.new.existing_method).to eq(5)
|
655
655
|
end
|
656
656
|
|
657
657
|
it "fails when the other expecations are not met" do
|
@@ -678,14 +678,14 @@ module RSpec
|
|
678
678
|
context "public methods" do
|
679
679
|
before(:each) do
|
680
680
|
klass.any_instance.stub(:existing_method).and_return(1)
|
681
|
-
klass.method_defined?(:__existing_method_without_any_instance__).
|
681
|
+
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_true
|
682
682
|
end
|
683
683
|
|
684
684
|
it "restores the class to its original state after each example when no instance is created" do
|
685
685
|
space.verify_all
|
686
686
|
|
687
|
-
klass.method_defined?(:__existing_method_without_any_instance__).
|
688
|
-
klass.new.existing_method.
|
687
|
+
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false
|
688
|
+
expect(klass.new.existing_method).to eq(existing_method_return_value)
|
689
689
|
end
|
690
690
|
|
691
691
|
it "restores the class to its original state after each example when one instance is created" do
|
@@ -693,8 +693,8 @@ module RSpec
|
|
693
693
|
|
694
694
|
space.verify_all
|
695
695
|
|
696
|
-
klass.method_defined?(:__existing_method_without_any_instance__).
|
697
|
-
klass.new.existing_method.
|
696
|
+
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false
|
697
|
+
expect(klass.new.existing_method).to eq(existing_method_return_value)
|
698
698
|
end
|
699
699
|
|
700
700
|
it "restores the class to its original state after each example when more than one instance is created" do
|
@@ -703,8 +703,8 @@ module RSpec
|
|
703
703
|
|
704
704
|
space.verify_all
|
705
705
|
|
706
|
-
klass.method_defined?(:__existing_method_without_any_instance__).
|
707
|
-
klass.new.existing_method.
|
706
|
+
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false
|
707
|
+
expect(klass.new.existing_method).to eq(existing_method_return_value)
|
708
708
|
end
|
709
709
|
end
|
710
710
|
|
@@ -715,15 +715,15 @@ module RSpec
|
|
715
715
|
end
|
716
716
|
|
717
717
|
it "cleans up the backed up method" do
|
718
|
-
klass.method_defined?(:__existing_method_without_any_instance__).
|
718
|
+
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false
|
719
719
|
end
|
720
720
|
|
721
721
|
it "restores a stubbed private method after the spec is run" do
|
722
|
-
klass.private_method_defined?(:private_method).
|
722
|
+
expect(klass.private_method_defined?(:private_method)).to be_true
|
723
723
|
end
|
724
724
|
|
725
725
|
it "ensures that the restored method behaves as it originally did" do
|
726
|
-
klass.new.send(:private_method).
|
726
|
+
expect(klass.new.send(:private_method)).to eq(:private_method_return_value)
|
727
727
|
end
|
728
728
|
end
|
729
729
|
end
|
@@ -737,15 +737,15 @@ module RSpec
|
|
737
737
|
end
|
738
738
|
|
739
739
|
it "cleans up the backed up method" do
|
740
|
-
klass.method_defined?(:__existing_method_without_any_instance__).
|
740
|
+
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false
|
741
741
|
end
|
742
742
|
|
743
743
|
it "restores a stubbed private method after the spec is run" do
|
744
|
-
klass.private_method_defined?(:private_method).
|
744
|
+
expect(klass.private_method_defined?(:private_method)).to be_true
|
745
745
|
end
|
746
746
|
|
747
747
|
it "ensures that the restored method behaves as it originally did" do
|
748
|
-
klass.new.send(:private_method).
|
748
|
+
expect(klass.new.send(:private_method)).to eq(:private_method_return_value)
|
749
749
|
end
|
750
750
|
end
|
751
751
|
|
@@ -757,7 +757,7 @@ module RSpec
|
|
757
757
|
end
|
758
758
|
|
759
759
|
it "second spec" do
|
760
|
-
klass.new.existing_method.
|
760
|
+
expect(klass.new.existing_method).to eq(existing_method_return_value)
|
761
761
|
end
|
762
762
|
end
|
763
763
|
|
@@ -772,7 +772,7 @@ module RSpec
|
|
772
772
|
end
|
773
773
|
|
774
774
|
it "second spec" do
|
775
|
-
@instance.existing_method.
|
775
|
+
expect(@instance.existing_method).to eq(existing_method_return_value)
|
776
776
|
end
|
777
777
|
end
|
778
778
|
end
|
@@ -782,7 +782,7 @@ module RSpec
|
|
782
782
|
klass.new.existing_method
|
783
783
|
space.verify_all
|
784
784
|
|
785
|
-
klass.new.existing_method.
|
785
|
+
expect(klass.new.existing_method).to eq(existing_method_return_value)
|
786
786
|
end
|
787
787
|
end
|
788
788
|
end
|
@@ -793,21 +793,21 @@ module RSpec
|
|
793
793
|
klass.any_instance.stub(:existing_method).and_return(true)
|
794
794
|
|
795
795
|
klass.rspec_verify
|
796
|
-
klass.new.
|
797
|
-
klass.new.existing_method.
|
796
|
+
expect(klass.new).to respond_to(:existing_method)
|
797
|
+
expect(klass.new.existing_method).to eq(existing_method_return_value)
|
798
798
|
end
|
799
799
|
end
|
800
800
|
|
801
801
|
it "adds an class to the current space when #any_instance is invoked" do
|
802
802
|
klass.any_instance
|
803
|
-
RSpec::Mocks::space.send(:receivers).
|
803
|
+
expect(RSpec::Mocks::space.send(:receivers)).to include(klass)
|
804
804
|
end
|
805
805
|
|
806
806
|
it "adds an instance to the current space when stubbed method is invoked" do
|
807
807
|
klass.any_instance.stub(:foo)
|
808
808
|
instance = klass.new
|
809
809
|
instance.foo
|
810
|
-
RSpec::Mocks::space.send(:receivers).
|
810
|
+
expect(RSpec::Mocks::space.send(:receivers)).to include(instance)
|
811
811
|
end
|
812
812
|
end
|
813
813
|
|
@@ -816,7 +816,7 @@ module RSpec
|
|
816
816
|
Object.any_instance.stub(:some_method)
|
817
817
|
o = Object.new
|
818
818
|
o.some_method
|
819
|
-
|
819
|
+
expect { o.dup.some_method }.to_not raise_error(SystemStackError)
|
820
820
|
end
|
821
821
|
|
822
822
|
it "doesn't bomb if the object doesn't support `dup`" do
|
@@ -834,7 +834,7 @@ module RSpec
|
|
834
834
|
|
835
835
|
klass.any_instance
|
836
836
|
|
837
|
-
|
837
|
+
expect { klass.new.dup('Dup dup dup') }.to_not raise_error(ArgumentError)
|
838
838
|
end
|
839
839
|
end
|
840
840
|
|
@@ -843,7 +843,7 @@ module RSpec
|
|
843
843
|
|
844
844
|
it "stubs the method correctly" do
|
845
845
|
klass.any_instance.stub(:existing_method).and_return("foo")
|
846
|
-
sub_klass.new.existing_method.
|
846
|
+
expect(sub_klass.new.existing_method).to eq "foo"
|
847
847
|
end
|
848
848
|
|
849
849
|
it "mocks the method correctly" do
|
@@ -862,12 +862,12 @@ module RSpec
|
|
862
862
|
|
863
863
|
it "stubs the method correctly" do
|
864
864
|
http_request_class.any_instance.stub(:existing_method).and_return("foo")
|
865
|
-
http_request_class.new.existing_method.
|
865
|
+
expect(http_request_class.new.existing_method).to eq "foo"
|
866
866
|
end
|
867
867
|
|
868
868
|
it "mocks the method correctly" do
|
869
869
|
http_request_class.any_instance.should_receive(:existing_method).and_return("foo")
|
870
|
-
http_request_class.new.existing_method.
|
870
|
+
expect(http_request_class.new.existing_method).to eq "foo"
|
871
871
|
end
|
872
872
|
end
|
873
873
|
|
@@ -876,11 +876,11 @@ module RSpec
|
|
876
876
|
klass.any_instance.stub(:existing_method).and_return(:stubbed_return_value)
|
877
877
|
|
878
878
|
instance = klass.new
|
879
|
-
instance.existing_method.
|
879
|
+
expect(instance.existing_method).to eq :stubbed_return_value
|
880
880
|
|
881
881
|
RSpec::Mocks.verify
|
882
882
|
|
883
|
-
instance.existing_method.
|
883
|
+
expect(instance.existing_method).to eq :existing_method_return_value
|
884
884
|
end
|
885
885
|
end
|
886
886
|
end
|