Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated allow_message_expectations_on_nil example method #1410

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Breaking Changes:
(Phil Pirozhkov, #1400)
* Change the default setting for `RSpec::Mocks::Configuration#verify_partial_doubles`
to `true`. (Phil Pirozhkov, #1409)
* Remove deprecated `allow_message_expectations_on_nil` example method.
(Phil Pirozhkov, #1410)

Bug Fixes:

Expand Down
10 changes: 0 additions & 10 deletions lib/rspec/mocks/example_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,6 @@ def class_spy(*args)
class_double(*args).as_null_object
end

# Disables warning messages about expectations being set on nil.
#
# By default warning messages are issued when expectations are set on
# nil. This is to prevent false-positives and to catch potential bugs
# early on.
# @deprecated Use {RSpec::Mocks::Configuration#allow_message_expectations_on_nil} instead.
def allow_message_expectations_on_nil
RSpec::Mocks.space.proxy_for(nil).warn_about_expectations = false
end

# Stubs the named constant with the given value.
# Like method stubs, the constant will be restored
# to its original value (or lack of one, if it was
Expand Down
47 changes: 10 additions & 37 deletions spec/rspec/mocks/nil_expectation_warning_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,27 @@ module Mocks
)).to_stderr
end

it 'does not issue a warning when expectations are set to be allowed' do
allow_message_expectations_on_nil

expect {
expect(nil).to receive(:foo)
expect(nil).to_not receive(:bar)
}.not_to output.to_stderr

nil.foo
end

context 'configured to allow expectation on nil' do
include_context 'with isolated configuration'

it 'does not issue a warning when expectations are set to be allowed' do
RSpec::Mocks.configuration.allow_message_expectations_on_nil = true
before { RSpec::Mocks.configuration.allow_message_expectations_on_nil = true }

it 'does not issue a warning when expectations are set to be allowed' do
expect {
expect(nil).to receive(:foo)
expect(nil).not_to receive(:bar)
}.not_to output.to_stderr

nil.foo
end

describe "marshalled `nil`" do
include_context "with monkey-patched marshal"

it 'doesnt error when marshalled' do
expect(Marshal.dump(nil)).to eq Marshal.dump_without_rspec_mocks(nil)
end
end
end

context 'configured to disallow expectations on nil' do
Expand All @@ -63,29 +60,5 @@ module Mocks
dbl.nil?
end
end

RSpec.describe "#allow_message_expectations_on_nil" do
include_context "with monkey-patched marshal"

it "does not affect subsequent examples" do
allow_message_expectations_on_nil
RSpec::Mocks.teardown
RSpec::Mocks.setup

expect {
expect(nil).to receive(:foo)
}.to output(a_string_including(
"An expectation of `:foo` was set on `nil`",
"#{__FILE__}:#{__LINE__ - 3}"
)).to_stderr

nil.foo
end

it 'doesnt error when marshalled' do
allow_message_expectations_on_nil
expect(Marshal.dump(nil)).to eq Marshal.dump_without_rspec_mocks(nil)
end
end
end
end
24 changes: 14 additions & 10 deletions spec/rspec/mocks/partial_double_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,21 @@ def call(name)
end
end

it "uses reports nil in the error message" do
allow_message_expectations_on_nil
context 'configured to allow expectation on nil' do
include_context 'with isolated configuration'

nil_var = nil
expect(nil_var).to receive(:foobar)
expect {
verify nil_var
}.to raise_error(
RSpec::Mocks::MockExpectationError,
%Q|(nil).foobar(*(any args))\n expected: 1 time with any arguments\n received: 0 times with any arguments|
)
it "uses reports nil in the error message" do
RSpec::Mocks.configuration.allow_message_expectations_on_nil = true

nil_var = nil
expect(nil_var).to receive(:foobar)
expect {
verify nil_var
}.to raise_error(
RSpec::Mocks::MockExpectationError,
%Q|(nil).foobar(*(any args))\n expected: 1 time with any arguments\n received: 0 times with any arguments|
)
end
end

it "includes the class name in the error when mocking a class method that is called an extra time with the wrong args" do
Expand Down