Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 91ddf83

Browse files
authored
Merge pull request #1409 from rspec/verify-partial-doubles-by-default
Verify partial doubles by default
2 parents eb5a621 + 43c1821 commit 91ddf83

File tree

10 files changed

+86
-33
lines changed

10 files changed

+86
-33
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Breaking Changes:
77
* Extract monkey-patching `should_receive`/`stub` syntax. (Phil Pirozhkov, #1365)
88
* Remove the deprecated `RSpec::Mocks::CannotSupportArgMutationsError` constant.
99
(Phil Pirozhkov, #1400)
10+
* Change the default setting for `RSpec::Mocks::Configuration#verify_partial_doubles`
11+
to `true`. (Phil Pirozhkov, #1409)
1012

1113
Bug Fixes:
1214

features/basics/partial_test_doubles.feature

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ Feature: Partial test doubles
2323
At the end of the example, RSpec verifies any message expectations, and then restores the
2424
original methods.
2525

26-
Note: we recommend enabling the [`verify_partial_doubles`](../verifying-doubles/partial-doubles) config option.
27-
2826
Scenario: Only the specified methods are redefined
2927
Given a file named "partial_double_spec.rb" with:
3028
"""ruby
Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
Feature: Partial doubles
22

3-
When the `verify_partial_doubles` configuration option is set, the same argument and
4-
method existence checks that are performed for [`object_double`](./using-an-object-double) are also performed on
5-
[partial doubles](../basics/partial-test-doubles). You should set this unless you have a good reason not to. It defaults to off
6-
only for backwards compatibility.
3+
The same argument and method existence checks that are performed for [`object_double`](./using-an-object-double) are also performed on
4+
[partial doubles](../basics/partial-test-doubles). You should only turn off `verify_partial_doubles` (by setting it to false)
5+
if you have a really good reason to.
76

87
Scenario: doubling an existing object
98
Given a file named "spec/user_spec.rb" with:
@@ -16,12 +15,6 @@ Feature: Partial doubles
1615
"saved!" if user.save
1716
end
1817
19-
RSpec.configure do |config|
20-
config.mock_with :rspec do |mocks|
21-
mocks.verify_partial_doubles = true
22-
end
23-
end
24-
2518
RSpec.describe '#save_user' do
2619
it 'renders message on success' do
2720
user = User.new
@@ -32,3 +25,28 @@ Feature: Partial doubles
3225
"""
3326
When I run `rspec spec/user_spec.rb`
3427
Then the output should contain "1 example, 1 failure"
28+
29+
Scenario: Stubbing a non-existent or a dynamic method
30+
Given a file named "spec/user_spec.rb" with:
31+
"""ruby
32+
RSpec.configure do |config|
33+
config.mock_with :rspec do |mocks|
34+
mocks.verify_partial_doubles = false
35+
end
36+
end
37+
38+
class Solution
39+
def self.best
40+
find_by_complexity
41+
end
42+
end
43+
44+
RSpec.describe '#find_by_complexity' do
45+
it 'finds a simple solution' do
46+
expect(Solution).to receive(:find_by_complexity).and_return("simple") # Method isn't defined
47+
expect(Solution.best).to eq("simple")
48+
end
49+
end
50+
"""
51+
When I run `rspec spec/user_spec.rb`
52+
Then the output should contain "1 example, 0 failures"

features/working_with_legacy_code/any_instance.feature

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ Feature: Any Instance
3131
"""ruby
3232
RSpec.describe "allow_any_instance_of" do
3333
it "returns the specified value on any instance of the class" do
34-
allow_any_instance_of(Object).to receive(:foo).and_return(:return_value)
34+
class Klass
35+
def foo; end
36+
end
37+
allow_any_instance_of(Klass).to receive(:foo).and_return(:return_value)
3538
36-
o = Object.new
39+
o = Klass.new
3740
expect(o.foo).to eq(:return_value)
3841
end
3942
end
@@ -47,9 +50,13 @@ Feature: Any Instance
4750
RSpec.describe "allow_any_instance_of" do
4851
context "with receive_messages" do
4952
it "stubs multiple methods" do
50-
allow_any_instance_of(Object).to receive_messages(:foo => 'foo', :bar => 'bar')
53+
class Klass
54+
def foo; end
55+
def bar; end
56+
end
57+
allow_any_instance_of(Klass).to receive_messages(:foo => 'foo', :bar => 'bar')
5158
52-
o = Object.new
59+
o = Klass.new
5360
expect(o.foo).to eq('foo')
5461
expect(o.bar).to eq('bar')
5562
end
@@ -65,10 +72,13 @@ Feature: Any Instance
6572
RSpec.describe "allow_any_instance_of" do
6673
context "with arguments" do
6774
it "returns the stubbed value when arguments match" do
68-
allow_any_instance_of(Object).to receive(:foo).with(:param_one, :param_two).and_return(:result_one)
69-
allow_any_instance_of(Object).to receive(:foo).with(:param_three, :param_four).and_return(:result_two)
75+
class Klass
76+
def foo(one, two); end
77+
end
78+
allow_any_instance_of(Klass).to receive(:foo).with(:param_one, :param_two).and_return(:result_one)
79+
allow_any_instance_of(Klass).to receive(:foo).with(:param_three, :param_four).and_return(:result_two)
7080
71-
o = Object.new
81+
o = Klass.new
7282
expect(o.foo(:param_one, :param_two)).to eq(:result_one)
7383
expect(o.foo(:param_three, :param_four)).to eq(:result_two)
7484
end
@@ -99,15 +109,21 @@ Feature: Any Instance
99109
"""ruby
100110
RSpec.describe "expect_any_instance_of" do
101111
before do
102-
expect_any_instance_of(Object).to receive(:foo)
112+
expect_any_instance_of(klass).to receive(:foo)
113+
end
114+
115+
let(:klass) do
116+
Class.new do
117+
def foo; end
118+
end
103119
end
104120
105121
it "passes when an instance receives the message" do
106-
Object.new.foo
122+
klass.new.foo
107123
end
108124
109125
it "fails when no instance receives the message" do
110-
Object.new.to_s
126+
klass.new.to_s
111127
end
112128
end
113129
"""

features/working_with_legacy_code/message_chains.feature

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Feature: Message Chains
77
allow(double).to receive_message_chain("foo.bar") { :baz }
88
allow(double).to receive_message_chain(:foo, :bar => :baz)
99
allow(double).to receive_message_chain(:foo, :bar) { :baz }
10-
````
10+
```
1111

1212
Given any of these three forms:
1313

@@ -74,19 +74,25 @@ Feature: Message Chains
7474
Given a file named "receive_message_chain_spec.rb" with:
7575
"""ruby
7676
RSpec.describe "Using receive_message_chain on any instance of a class" do
77+
let(:klass) do
78+
Class.new do
79+
def foo; end
80+
end
81+
end
82+
7783
example "using a string and a block" do
78-
allow_any_instance_of(Object).to receive_message_chain("foo.bar") { :baz }
79-
expect(Object.new.foo.bar).to eq(:baz)
84+
allow_any_instance_of(klass).to receive_message_chain("foo.bar") { :baz }
85+
expect(klass.new.foo.bar).to eq(:baz)
8086
end
8187
8288
example "using symbols and a hash" do
83-
allow_any_instance_of(Object).to receive_message_chain(:foo, :bar => :baz)
84-
expect(Object.new.foo.bar).to eq(:baz)
89+
allow_any_instance_of(klass).to receive_message_chain(:foo, :bar => :baz)
90+
expect(klass.new.foo.bar).to eq(:baz)
8591
end
8692
8793
example "using symbols and a block" do
88-
allow_any_instance_of(Object).to receive_message_chain(:foo, :bar) { :baz }
89-
expect(Object.new.foo.bar).to eq(:baz)
94+
allow_any_instance_of(klass).to receive_message_chain(:foo, :bar) { :baz }
95+
expect(klass.new.foo.bar).to eq(:baz)
9096
end
9197
end
9298
"""

lib/rspec/mocks/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def initialize
77
@yield_receiver_to_any_instance_implementation_blocks = true
88
@verify_doubled_constant_names = false
99
@transfer_nested_constants = false
10-
@verify_partial_doubles = false
10+
@verify_partial_doubles = true
1111
@temporarily_suppress_partial_double_verification = false
1212
@color = false
1313
end

spec/rspec/mocks/any_instance_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,13 @@ def foo; end
11661166
end
11671167

11681168
context "passing the receiver to the implementation block" do
1169+
let(:klass) do
1170+
Class.new do
1171+
def bees(arg)
1172+
end
1173+
end
1174+
end
1175+
11691176
context "when configured to pass the instance" do
11701177
include_context 'with isolated configuration'
11711178
before(:each) do

spec/rspec/mocks/stub_implementation_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ def obj.foo; :original; end
8484
end
8585

8686
context "when partial doubles are not verified" do
87-
before { expect(RSpec::Mocks.configuration.verify_partial_doubles?).to be false }
87+
include_context "with isolated configuration"
88+
before { RSpec::Mocks.configuration.verify_partial_doubles = false }
8889
include_examples "stubbing `new` on class objects"
8990
end
9091

spec/rspec/mocks/verifying_doubles/class_double_with_class_not_loaded_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ module Mocks
2323
end
2424

2525
specify "trying to raise a class_double raises a TypeError" do
26-
subject = Object.new
26+
subject = Class.new do
27+
def some_method; end
28+
end.new
2729
class_double("StubbedError").as_stubbed_const
2830
allow(subject).to receive(:some_method).and_raise(StubbedError)
2931
expect { subject.some_method }.to raise_error(TypeError, 'exception class/object expected')

spec/spec_helper.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ def self.fake_matcher_description
7777
require 'rspec/support/spec'
7878

7979
RSpec.configure do |config|
80-
config.mock_with :rspec
80+
config.mock_with :rspec do |mocks|
81+
mocks.verify_partial_doubles = false
82+
end
83+
8184
config.color = true
8285
config.order = :random
8386

0 commit comments

Comments
 (0)