Skip to content

Commit

Permalink
Add RSpec 4 deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
pirj committed Mar 15, 2021
1 parent 97c972b commit f7da8b4
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 19 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Bug Fixes:
* Support keyword argument semantics when constraining argument expectations using
`with` on Ruby 3.0+ (Yusuke Endoh, #1394)

Deprecations:

* Add RSpec 4 deprecation warnings. (Phil Pirozhkov, #1418)

### 3.10.2 / 2021-01-27
[Full Changelog](http://github.com/rspec/rspec-mocks/compare/v3.10.1...v3.10.2)

Expand Down
23 changes: 23 additions & 0 deletions lib/rspec/mocks/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,23 @@ def add_stub_and_should_receive_to(*modules)
#
def syntax=(*values)
syntaxes = values.flatten
if self.class.warn_about_syntax?
RSpec.deprecate('Mocks syntax configuration',
:replacement => 'the default `expect` syntax',
:call_site => nil)
end
if syntaxes.include?(:expect)
Syntax.enable_expect
else
Syntax.disable_expect
end

if syntaxes.include?(:should)
if self.class.warn_about_syntax?
RSpec.deprecate('`:should` Mocks syntax',
:replacement => 'the default `expect` syntax',
:call_site => nil)
end
Syntax.enable_should
else
Syntax.disable_should
Expand Down Expand Up @@ -199,6 +209,18 @@ def reset_syntaxes_to_default
self.syntax = [:should, :expect]
RSpec::Mocks::Syntax.warn_about_should!
end

# @api private
def self.warn_about_syntax?
@warn_about_syntax
end

# @api private
def self.warn_about_syntax!
@warn_about_syntax = true
end

@warn_about_syntax = false
end

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

configuration.reset_syntaxes_to_default
Configuration.warn_about_syntax!
end
end
2 changes: 2 additions & 0 deletions lib/rspec/mocks/example_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ def class_spy(*args)
# early on.
# @deprecated Use {RSpec::Mocks::Configuration#allow_message_expectations_on_nil} instead.
def allow_message_expectations_on_nil
RSpec.deprecate("`allow_message_expectations_on_nil` example method",
:replacement => "`allow_message_expectations_on_nil` configuration option")
RSpec::Mocks.space.proxy_for(nil).warn_about_expectations = false
end

Expand Down
22 changes: 17 additions & 5 deletions spec/rspec/mocks/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ def sandboxed
expect(::RSpec::Mocks::ExampleMethods).not_to receive(:method_added)
configure_syntax :expect
end

it 'emits a deprecation warning' do
expect_deprecation_without_call_site(/Mocks syntax configuration/)
configure_syntax :expect
end
end

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

it "does not warn about the should syntax" do
RSpec.should_not_receive(:deprecate)
Object.new.should_not_receive(:bees)
end

it 'is a no-op when configured a second time' do
Syntax.default_should_syntax_host.should_not_receive(:method_added)
::RSpec::Mocks::ExampleMethods.should_not_receive(:method_undefined)
end

it 'emits two deprecation warnings' do
configure_syntax :expect
expect_deprecation_without_call_site(/`:should` Mocks syntax/)
expect_deprecation_without_call_site(/Mocks syntax configuration/)
configure_syntax :should
end
end
Expand Down Expand Up @@ -124,6 +130,12 @@ def sandboxed
expect(RSpec).not_to receive(:deprecate)
expect(Object.new).not_to receive(:bees)
end

it 'emits two deprecation warnings' do
expect_deprecation_without_call_site(/`:should` Mocks syntax/)
expect_deprecation_without_call_site(/Mocks syntax configuration/)
configure_syntax [:should, :expect]
end
end
end

Expand Down
11 changes: 11 additions & 0 deletions spec/rspec/mocks/example_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ def test_extend_on_new_object(*to_extend, &block)
expect(dbl.foo).to eq(1)
end
end

describe '#allow_message_expectations_on_nil' do
it "emits a deprecation warning on use" do
expect_deprecation_with_call_site(__FILE__, __LINE__ + 3, /allow_message_expectations_on_nil/)
RSpec.describe do
it do
allow_message_expectations_on_nil
end
end.run
end
end
end
end
end
10 changes: 0 additions & 10 deletions spec/rspec/mocks/should_syntax_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -551,16 +551,6 @@ def use_rspec_mocks
o2.unstub(:faces)
end

it "doesn't warn about stubbing after a reset and setting should" do
expect(RSpec).not_to receive(:deprecate)
RSpec::Mocks.configuration.reset_syntaxes_to_default
RSpec::Mocks.configuration.syntax = :should
o = Object.new
o2 = Object.new
o.stub(:faces)
o2.stub(:faces)
end

it "includes the call site in the deprecation warning" do
obj = Object.new
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1)
Expand Down
5 changes: 1 addition & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ def self.fake_matcher_description
expectations.syntax = :expect
end

config.mock_with :rspec do |mocks|
$default_rspec_mocks_syntax = mocks.syntax
mocks.syntax = :expect
end
$default_rspec_mocks_syntax = [:should, :expect]

old_verbose = nil
config.before(:each, :silence_warnings) do
Expand Down

0 comments on commit f7da8b4

Please sign in to comment.