rspec-mocks 2.14.1 → 2.14.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog.md +9 -2
- data/features/method_stubs/to_ary.feature +8 -4
- data/lib/rspec/mocks/syntax.rb +17 -7
- data/lib/rspec/mocks/test_double.rb +9 -3
- data/lib/rspec/mocks/version.rb +1 -1
- data/spec/rspec/mocks/mock_spec.rb +21 -0
- data/spec/rspec/mocks/stub_spec.rb +14 -1
- data/spec/rspec/mocks/to_ary_spec.rb +24 -10
- metadata +5 -5
data/Changelog.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
|
-
### 2.14.2
|
2
|
-
[full changelog](http://github.com/rspec/rspec-mocks/compare/v2.14.1...2
|
1
|
+
### 2.14.2 / 2013-07-30
|
2
|
+
[full changelog](http://github.com/rspec/rspec-mocks/compare/v2.14.1...v2.14.2)
|
3
|
+
|
4
|
+
Bug Fixes:
|
5
|
+
|
6
|
+
* Fix `as_null_object` doubles so that they return `nil` from `to_ary`
|
7
|
+
(Jon Rowe).
|
8
|
+
* Fix regression in 2.14 that made `stub!` (with an implicit receiver)
|
9
|
+
return a test double rather than stub a method (Myron Marston).
|
3
10
|
|
4
11
|
### 2.14.1 / 2013-07-07
|
5
12
|
[full changelog](http://github.com/rspec/rspec-mocks/compare/v2.14.0...v2.14.1)
|
@@ -17,10 +17,6 @@ Feature: double handling to_ary
|
|
17
17
|
"""ruby
|
18
18
|
describe "#to_ary" do
|
19
19
|
shared_examples "to_ary" do
|
20
|
-
it "raises a NoMethodError" do
|
21
|
-
expect { obj.to_ary }.to raise_error(NoMethodError)
|
22
|
-
end
|
23
|
-
|
24
20
|
it "can be overridden with a stub" do
|
25
21
|
obj.stub(:to_ary) { :non_nil_value }
|
26
22
|
obj.to_ary.should be(:non_nil_value)
|
@@ -35,11 +31,19 @@ Feature: double handling to_ary
|
|
35
31
|
context "sent to a double as_null_object" do
|
36
32
|
let(:obj) { double('obj').as_null_object }
|
37
33
|
include_examples "to_ary"
|
34
|
+
|
35
|
+
it "returns nil" do
|
36
|
+
expect( obj.to_ary ).to eq nil
|
37
|
+
end
|
38
38
|
end
|
39
39
|
|
40
40
|
context "sent to a double without as_null_object" do
|
41
41
|
let(:obj) { double('obj') }
|
42
42
|
include_examples "to_ary"
|
43
|
+
|
44
|
+
it "raises a NoMethodError" do
|
45
|
+
expect { obj.to_ary }.to raise_error(NoMethodError)
|
46
|
+
end
|
43
47
|
end
|
44
48
|
end
|
45
49
|
"""
|
data/lib/rspec/mocks/syntax.rb
CHANGED
@@ -4,6 +4,21 @@ module RSpec
|
|
4
4
|
# Provides methods for enabling and disabling the available syntaxes
|
5
5
|
# provided by rspec-mocks.
|
6
6
|
module Syntax
|
7
|
+
# @api private
|
8
|
+
#
|
9
|
+
# Common stubbing logic for both `stub` and `stub!`. This used to
|
10
|
+
# live in `stub`, and `stub!` delegated to `stub`, but we discovered
|
11
|
+
# that `stub!` was delegating to `RSpec::Mocks::ExampleMethods#stub`
|
12
|
+
# (which declares a test double) when called with an implicit receiver,
|
13
|
+
# which was a regression in 2.14.0.
|
14
|
+
def self.stub_object(object, message_or_hash, opts = {}, &block)
|
15
|
+
if ::Hash === message_or_hash
|
16
|
+
message_or_hash.each {|message, value| stub_object(object, message).and_return value }
|
17
|
+
else
|
18
|
+
opts[:expected_from] = caller(1)[1]
|
19
|
+
::RSpec::Mocks.allow_message(object, message_or_hash, opts, &block)
|
20
|
+
end
|
21
|
+
end
|
7
22
|
|
8
23
|
# @api private
|
9
24
|
# Enables the should syntax (`dbl.stub`, `dbl.should_receive`, etc).
|
@@ -22,12 +37,7 @@ module RSpec
|
|
22
37
|
end
|
23
38
|
|
24
39
|
def stub(message_or_hash, opts={}, &block)
|
25
|
-
|
26
|
-
message_or_hash.each {|message, value| stub(message).and_return value }
|
27
|
-
else
|
28
|
-
opts[:expected_from] = caller(1)[0]
|
29
|
-
::RSpec::Mocks.allow_message(self, message_or_hash, opts, &block)
|
30
|
-
end
|
40
|
+
::RSpec::Mocks::Syntax.stub_object(self, message_or_hash, opts, &block)
|
31
41
|
end
|
32
42
|
|
33
43
|
def unstub(message)
|
@@ -36,7 +46,7 @@ module RSpec
|
|
36
46
|
|
37
47
|
def stub!(message_or_hash, opts={}, &block)
|
38
48
|
::RSpec.deprecate "stub!", :replacement => "stub"
|
39
|
-
|
49
|
+
::RSpec::Mocks::Syntax.stub_object(self, message_or_hash, opts, &block)
|
40
50
|
end
|
41
51
|
|
42
52
|
def unstub!(message)
|
@@ -58,7 +58,7 @@ module RSpec
|
|
58
58
|
|
59
59
|
# @private
|
60
60
|
def respond_to?(message, incl_private=false)
|
61
|
-
__mock_proxy.null_object?
|
61
|
+
__mock_proxy.null_object? ? true : super
|
62
62
|
end
|
63
63
|
|
64
64
|
# @private
|
@@ -84,13 +84,19 @@ module RSpec
|
|
84
84
|
end
|
85
85
|
|
86
86
|
def method_missing(message, *args, &block)
|
87
|
-
|
88
|
-
|
87
|
+
if __mock_proxy.null_object?
|
88
|
+
case message
|
89
|
+
when :to_int then return 0
|
90
|
+
when :to_a, :to_ary then return nil
|
91
|
+
end
|
92
|
+
end
|
89
93
|
__mock_proxy.record_message_received(message, *args, &block)
|
90
94
|
|
91
95
|
begin
|
92
96
|
__mock_proxy.null_object? ? self : super
|
93
97
|
rescue NameError
|
98
|
+
# Required wrapping doubles in an Array on Ruby 1.9.2
|
99
|
+
raise NoMethodError if [:to_a, :to_ary].include? message
|
94
100
|
__mock_proxy.raise_unexpected_message_error(message, *args)
|
95
101
|
end
|
96
102
|
end
|
data/lib/rspec/mocks/version.rb
CHANGED
@@ -227,6 +227,27 @@ module RSpec
|
|
227
227
|
expect( Array(@double) ).to eq([@double])
|
228
228
|
end
|
229
229
|
|
230
|
+
it "is wrappable in an array when a null object" do
|
231
|
+
expect( Array(@double.as_null_object) ).to eq [@double]
|
232
|
+
end
|
233
|
+
|
234
|
+
it "responds to to_ary as a null object" do
|
235
|
+
expect(@double.as_null_object.to_ary).to eq nil
|
236
|
+
end
|
237
|
+
|
238
|
+
it "responds to to_a as a null object" do
|
239
|
+
if RUBY_VERSION.to_f > 1.8
|
240
|
+
expect(@double.as_null_object.to_a).to eq nil
|
241
|
+
else
|
242
|
+
expect(@double.as_null_object.to_a).to eq [@double]
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
it "passes proc to expectation block without an argument" do
|
247
|
+
@double.should_receive(:foo) { |&block| expect(block.call).to eq(:bar) }
|
248
|
+
@double.foo { :bar }
|
249
|
+
end
|
250
|
+
|
230
251
|
context "with Ruby > 1.8.6", :unless => RUBY_VERSION.to_s == '1.8.6' do
|
231
252
|
it "passes proc to expectation block without an argument" do
|
232
253
|
# We eval this because Ruby 1.8.6's syntax parser barfs on { |&block| ... }
|
@@ -38,12 +38,25 @@ module RSpec
|
|
38
38
|
end
|
39
39
|
|
40
40
|
describe "using stub!" do
|
41
|
-
|
41
|
+
before do
|
42
|
+
allow(RSpec).to receive(:deprecate)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "warns of deprecation" do
|
42
46
|
expect(RSpec).to receive(:deprecate).with("stub!", :replacement => "stub")
|
43
47
|
@instance.stub!(:msg).and_return(:return_value)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "returns the declared value when the message is received" do
|
51
|
+
@instance.stub!(:msg).and_return(:return_value)
|
44
52
|
expect(@instance.msg).to equal(:return_value)
|
45
53
|
verify @instance
|
46
54
|
end
|
55
|
+
|
56
|
+
it "can be used to stub the example context itself (since `stub` returns a test dobule instead)" do
|
57
|
+
stub!(:foo).and_return(5)
|
58
|
+
expect(foo).to eq(5)
|
59
|
+
end
|
47
60
|
end
|
48
61
|
|
49
62
|
describe 'using unstub' do
|
@@ -2,16 +2,6 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe "a double receiving to_ary" do
|
4
4
|
shared_examples "to_ary" do
|
5
|
-
it "returns nil" do
|
6
|
-
expect do
|
7
|
-
expect(obj.to_ary).to be_nil
|
8
|
-
end.to raise_error(NoMethodError)
|
9
|
-
end
|
10
|
-
|
11
|
-
it "doesn't respond" do
|
12
|
-
expect(obj).not_to respond_to(:to_ary)
|
13
|
-
end
|
14
|
-
|
15
5
|
it "can be overridden with a stub" do
|
16
6
|
obj.stub(:to_ary) { :non_nil_value }
|
17
7
|
expect(obj.to_ary).to be(:non_nil_value)
|
@@ -31,10 +21,34 @@ describe "a double receiving to_ary" do
|
|
31
21
|
context "double as_null_object" do
|
32
22
|
let(:obj) { double('obj').as_null_object }
|
33
23
|
include_examples "to_ary"
|
24
|
+
|
25
|
+
it "does respond to to_ary" do
|
26
|
+
expect(obj).to respond_to(:to_ary)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "does respond to to_a" do
|
30
|
+
expect(obj).to respond_to(:to_a)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "returns nil" do
|
34
|
+
expect(obj.to_ary).to eq nil
|
35
|
+
end
|
34
36
|
end
|
35
37
|
|
36
38
|
context "double without as_null_object" do
|
37
39
|
let(:obj) { double('obj') }
|
38
40
|
include_examples "to_ary"
|
41
|
+
|
42
|
+
it "doesn't respond to to_ary" do
|
43
|
+
expect(obj).not_to respond_to(:to_ary)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "doesn't respond to to_a", :if => ( RUBY_VERSION.to_f > 1.8 ) do
|
47
|
+
expect(obj).not_to respond_to(:to_a)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "raises " do
|
51
|
+
expect { obj.to_ary }.to raise_error(NoMethodError)
|
52
|
+
end
|
39
53
|
end
|
40
54
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: rspec-mocks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 2.14.
|
5
|
+
version: 2.14.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Steven Baker
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-07-
|
13
|
+
date: 2013-07-30 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -214,7 +214,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
214
214
|
version: '0'
|
215
215
|
segments:
|
216
216
|
- 0
|
217
|
-
hash:
|
217
|
+
hash: 3181053517643959284
|
218
218
|
none: false
|
219
219
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
220
220
|
requirements:
|
@@ -223,14 +223,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
223
223
|
version: '0'
|
224
224
|
segments:
|
225
225
|
- 0
|
226
|
-
hash:
|
226
|
+
hash: 3181053517643959284
|
227
227
|
none: false
|
228
228
|
requirements: []
|
229
229
|
rubyforge_project: rspec
|
230
230
|
rubygems_version: 1.8.24
|
231
231
|
signing_key:
|
232
232
|
specification_version: 3
|
233
|
-
summary: rspec-mocks-2.14.
|
233
|
+
summary: rspec-mocks-2.14.2
|
234
234
|
test_files:
|
235
235
|
- features/README.md
|
236
236
|
- features/Scope.md
|