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

Commit f7da8b4

Browse files
committed
Add RSpec 4 deprecations
1 parent 97c972b commit f7da8b4

File tree

7 files changed

+58
-19
lines changed

7 files changed

+58
-19
lines changed

Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Bug Fixes:
1111
* Support keyword argument semantics when constraining argument expectations using
1212
`with` on Ruby 3.0+ (Yusuke Endoh, #1394)
1313

14+
Deprecations:
15+
16+
* Add RSpec 4 deprecation warnings. (Phil Pirozhkov, #1418)
17+
1418
### 3.10.2 / 2021-01-27
1519
[Full Changelog](http://github.com/rspec/rspec-mocks/compare/v3.10.1...v3.10.2)
1620

lib/rspec/mocks/configuration.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,23 @@ def add_stub_and_should_receive_to(*modules)
8080
#
8181
def syntax=(*values)
8282
syntaxes = values.flatten
83+
if self.class.warn_about_syntax?
84+
RSpec.deprecate('Mocks syntax configuration',
85+
:replacement => 'the default `expect` syntax',
86+
:call_site => nil)
87+
end
8388
if syntaxes.include?(:expect)
8489
Syntax.enable_expect
8590
else
8691
Syntax.disable_expect
8792
end
8893

8994
if syntaxes.include?(:should)
95+
if self.class.warn_about_syntax?
96+
RSpec.deprecate('`:should` Mocks syntax',
97+
:replacement => 'the default `expect` syntax',
98+
:call_site => nil)
99+
end
90100
Syntax.enable_should
91101
else
92102
Syntax.disable_should
@@ -199,6 +209,18 @@ def reset_syntaxes_to_default
199209
self.syntax = [:should, :expect]
200210
RSpec::Mocks::Syntax.warn_about_should!
201211
end
212+
213+
# @api private
214+
def self.warn_about_syntax?
215+
@warn_about_syntax
216+
end
217+
218+
# @api private
219+
def self.warn_about_syntax!
220+
@warn_about_syntax = true
221+
end
222+
223+
@warn_about_syntax = false
202224
end
203225

204226
# Mocks specific configuration, as distinct from `RSpec.configuration`
@@ -208,5 +230,6 @@ def self.configuration
208230
end
209231

210232
configuration.reset_syntaxes_to_default
233+
Configuration.warn_about_syntax!
211234
end
212235
end

lib/rspec/mocks/example_methods.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ def class_spy(*args)
199199
# early on.
200200
# @deprecated Use {RSpec::Mocks::Configuration#allow_message_expectations_on_nil} instead.
201201
def allow_message_expectations_on_nil
202+
RSpec.deprecate("`allow_message_expectations_on_nil` example method",
203+
:replacement => "`allow_message_expectations_on_nil` configuration option")
202204
RSpec::Mocks.space.proxy_for(nil).warn_about_expectations = false
203205
end
204206

spec/rspec/mocks/configuration_spec.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ def sandboxed
6868
expect(::RSpec::Mocks::ExampleMethods).not_to receive(:method_added)
6969
configure_syntax :expect
7070
end
71+
72+
it 'emits a deprecation warning' do
73+
expect_deprecation_without_call_site(/Mocks syntax configuration/)
74+
configure_syntax :expect
75+
end
7176
end
7277

7378
context 'when configured to :should' do
@@ -89,14 +94,15 @@ def sandboxed
8994
expect(configured_syntax).to eq([:should])
9095
end
9196

92-
it "does not warn about the should syntax" do
93-
RSpec.should_not_receive(:deprecate)
94-
Object.new.should_not_receive(:bees)
95-
end
96-
9797
it 'is a no-op when configured a second time' do
9898
Syntax.default_should_syntax_host.should_not_receive(:method_added)
9999
::RSpec::Mocks::ExampleMethods.should_not_receive(:method_undefined)
100+
end
101+
102+
it 'emits two deprecation warnings' do
103+
configure_syntax :expect
104+
expect_deprecation_without_call_site(/`:should` Mocks syntax/)
105+
expect_deprecation_without_call_site(/Mocks syntax configuration/)
100106
configure_syntax :should
101107
end
102108
end
@@ -124,6 +130,12 @@ def sandboxed
124130
expect(RSpec).not_to receive(:deprecate)
125131
expect(Object.new).not_to receive(:bees)
126132
end
133+
134+
it 'emits two deprecation warnings' do
135+
expect_deprecation_without_call_site(/`:should` Mocks syntax/)
136+
expect_deprecation_without_call_site(/Mocks syntax configuration/)
137+
configure_syntax [:should, :expect]
138+
end
127139
end
128140
end
129141

spec/rspec/mocks/example_methods_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ def test_extend_on_new_object(*to_extend, &block)
3333
expect(dbl.foo).to eq(1)
3434
end
3535
end
36+
37+
describe '#allow_message_expectations_on_nil' do
38+
it "emits a deprecation warning on use" do
39+
expect_deprecation_with_call_site(__FILE__, __LINE__ + 3, /allow_message_expectations_on_nil/)
40+
RSpec.describe do
41+
it do
42+
allow_message_expectations_on_nil
43+
end
44+
end.run
45+
end
46+
end
3647
end
3748
end
3849
end

spec/rspec/mocks/should_syntax_spec.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -551,16 +551,6 @@ def use_rspec_mocks
551551
o2.unstub(:faces)
552552
end
553553

554-
it "doesn't warn about stubbing after a reset and setting should" do
555-
expect(RSpec).not_to receive(:deprecate)
556-
RSpec::Mocks.configuration.reset_syntaxes_to_default
557-
RSpec::Mocks.configuration.syntax = :should
558-
o = Object.new
559-
o2 = Object.new
560-
o.stub(:faces)
561-
o2.stub(:faces)
562-
end
563-
564554
it "includes the call site in the deprecation warning" do
565555
obj = Object.new
566556
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1)

spec/spec_helper.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,7 @@ def self.fake_matcher_description
8787
expectations.syntax = :expect
8888
end
8989

90-
config.mock_with :rspec do |mocks|
91-
$default_rspec_mocks_syntax = mocks.syntax
92-
mocks.syntax = :expect
93-
end
90+
$default_rspec_mocks_syntax = [:should, :expect]
9491

9592
old_verbose = nil
9693
config.before(:each, :silence_warnings) do

0 commit comments

Comments
 (0)