Skip to content

Commit

Permalink
Default reinstate_undocumented_behaviour_from_v1_9 to false
Browse files Browse the repository at this point in the history
Behaviour which relies on the Mocha v1.9 behaviour will have been
highlighted by deprecation warnings and if these have not been fixed
it's still possible to set the
reinstate_undocumented_behaviour_from_v1_9 configuration option to true.
  • Loading branch information
floehopper committed Oct 18, 2022
1 parent dc8ca96 commit 6fcaf94
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 51 deletions.
4 changes: 2 additions & 2 deletions lib/mocha/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Configuration
:stubbing_non_public_method => :allow,
:stubbing_method_on_nil => :prevent,
:display_matching_invocations_on_failure => false,
:reinstate_undocumented_behaviour_from_v1_9 => true
:reinstate_undocumented_behaviour_from_v1_9 => false
}.freeze

attr_reader :options
Expand Down Expand Up @@ -258,7 +258,7 @@ def display_matching_invocations_on_failure?
#
# Enabling this configuration option reinstates the previous behaviour, but displays a deprecation warning.
#
# @param [Boolean] value +true+ to reinstate undocumented behaviour; enabled by default.
# @param [Boolean] value +true+ to reinstate undocumented behaviour; disabled by default.
#
# @example Reinstate undocumented behaviour for {API#mock}
# Mocha.configure do |c|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ def teardown

def test_mock_built_with_single_symbol_argument_with_satisfied_expectation
test_result = run_as_test do
DeprecationDisabler.disable_deprecations do
m = mock(:my_method)
assert_nil m.my_method
Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do
DeprecationDisabler.disable_deprecations do
m = mock(:my_method)
assert_nil m.my_method
end
end
expected_warning = 'Explicitly include `my_method` in Hash of expected methods vs return values, e.g. `mock(:my_method => nil)`.'
assert_equal expected_warning, Mocha::Deprecation.messages.last
Expand All @@ -26,8 +28,10 @@ def test_mock_built_with_single_symbol_argument_with_satisfied_expectation

def test_mock_built_with_single_symbol_argument_with_unsatisfied_expectation
test_result = run_as_test do
DeprecationDisabler.disable_deprecations do
mock(:my_method)
Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do
DeprecationDisabler.disable_deprecations do
mock(:my_method)
end
end
end
assert_failed(test_result)
Expand All @@ -38,9 +42,11 @@ def test_mock_built_with_single_symbol_argument_with_unsatisfied_expectation

def test_stub_built_with_single_symbol_argument
test_result = run_as_test do
DeprecationDisabler.disable_deprecations do
s = stub(:my_method)
assert_nil s.my_method
Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do
DeprecationDisabler.disable_deprecations do
s = stub(:my_method)
assert_nil s.my_method
end
end
expected_warning = 'Explicitly include `my_method` in Hash of stubbed methods vs return values, e.g. `stub(:my_method => nil)`.'
assert_equal expected_warning, Mocha::Deprecation.messages.last
Expand All @@ -50,9 +56,11 @@ def test_stub_built_with_single_symbol_argument

def test_mock_built_with_first_argument_a_symbol_and_second_argument_a_hash
test_result = run_as_test do
DeprecationDisabler.disable_deprecations do
s = mock(:my_method, :another_method => 123)
assert_nil s.my_method
Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do
DeprecationDisabler.disable_deprecations do
s = mock(:my_method, :another_method => 123)
assert_nil s.my_method
end
end
expected_warning = 'In this case the 2nd argument for `mock(:#my_method, ...)` is ignored, but in the future a Hash of expected methods vs return values will be respected.'
assert Mocha::Deprecation.messages.last(2).include?(expected_warning)
Expand All @@ -62,9 +70,11 @@ def test_mock_built_with_first_argument_a_symbol_and_second_argument_a_hash

def test_stub_built_with_first_argument_a_symbol_and_second_argument_a_hash
test_result = run_as_test do
DeprecationDisabler.disable_deprecations do
s = stub(:my_method, :another_method => 123)
assert_nil s.my_method
Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do
DeprecationDisabler.disable_deprecations do
s = stub(:my_method, :another_method => 123)
assert_nil s.my_method
end
end
expected_warning = 'In this case the 2nd argument for `stub(:#my_method, ...)` is ignored, but in the future a Hash of stubbed methods vs return values will be respected.'
assert Mocha::Deprecation.messages.last(2).include?(expected_warning)
Expand All @@ -74,9 +84,11 @@ def test_stub_built_with_first_argument_a_symbol_and_second_argument_a_hash

def test_stub_everything_built_with_single_symbol_argument
test_result = run_as_test do
DeprecationDisabler.disable_deprecations do
s = stub_everything(:my_method)
assert_nil s.my_method
Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do
DeprecationDisabler.disable_deprecations do
s = stub_everything(:my_method)
assert_nil s.my_method
end
end
expected_warning = 'Explicitly include `my_method` in Hash of stubbed methods vs return values, e.g. `stub_everything(:my_method => nil)`.'
assert_equal expected_warning, Mocha::Deprecation.messages.last
Expand All @@ -86,9 +98,11 @@ def test_stub_everything_built_with_single_symbol_argument

def test_stub_everything_built_with_first_argument_a_symbol_and_second_argument_a_hash
test_result = run_as_test do
DeprecationDisabler.disable_deprecations do
s = stub_everything(:my_method, :another_method => 123)
assert_nil s.my_method
Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do
DeprecationDisabler.disable_deprecations do
s = stub_everything(:my_method, :another_method => 123)
assert_nil s.my_method
end
end
expected_warning = 'In this case the 2nd argument for `stub_everything(:#my_method, ...)` is ignored, but in the future a Hash of stubbed methods vs return values will be respected.' # rubocop:disable Metrics/LineLength
assert Mocha::Deprecation.messages.last(2).include?(expected_warning)
Expand Down
34 changes: 12 additions & 22 deletions test/acceptance/mock_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ def test_should_build_string_named_mock_and_explicitly_add_an_expectation_which_

def test_should_build_symbol_named_mock_and_explicitly_add_an_expectation_which_is_satisfied
test_result = run_as_test do
Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => false) do
foo = mock(:foo)
foo.expects(:bar)
foo.bar
end
foo = mock(:foo)
foo.expects(:bar)
foo.bar
end
assert_passed(test_result)
end
Expand All @@ -62,10 +60,8 @@ def test_should_build_string_named_mock_and_explicitly_add_an_expectation_which_

def test_should_build_symbol_named_mock_and_explicitly_add_an_expectation_which_is_not_satisfied
test_result = run_as_test do
DeprecationDisabler.disable_deprecations do
foo = mock(:foo)
foo.expects(:bar)
end
foo = mock(:foo)
foo.expects(:bar)
end
assert_failed(test_result)
end
Expand Down Expand Up @@ -106,11 +102,9 @@ def test_should_build_string_named_mock_incorporating_two_expectations_which_are

def test_should_build_symbol_named_mock_incorporating_two_expectations_which_are_satisifed
test_result = run_as_test do
Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => false) do
foo = mock(:foo, :bar => 'bar', :baz => 'baz')
foo.bar
foo.baz
end
foo = mock(:foo, :bar => 'bar', :baz => 'baz')
foo.bar
foo.baz
end
assert_passed(test_result)
end
Expand All @@ -125,10 +119,8 @@ def test_should_build_string_named_mock_incorporating_two_expectations_the_first

def test_should_build_symbol_named_mock_incorporating_two_expectations_the_first_of_which_is_not_satisifed
test_result = run_as_test do
DeprecationDisabler.disable_deprecations do
foo = mock(:foo, :bar => 'bar', :baz => 'baz')
foo.baz
end
foo = mock(:foo, :bar => 'bar', :baz => 'baz')
foo.baz
end
assert_failed(test_result)
end
Expand All @@ -143,10 +135,8 @@ def test_should_build_string_named_mock_incorporating_two_expectations_the_secon

def test_should_build_symbol_named_mock_incorporating_two_expectations_the_second_of_which_is_not_satisifed
test_result = run_as_test do
DeprecationDisabler.disable_deprecations do
foo = mock(:foo, :bar => 'bar', :baz => 'baz')
foo.bar
end
foo = mock(:foo, :bar => 'bar', :baz => 'baz')
foo.bar
end
assert_failed(test_result)
end
Expand Down
16 changes: 9 additions & 7 deletions test/unit/expectation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ def test_should_yield_no_parameters
end

def test_yield_should_fail_when_the_caller_does_not_provide_a_block_and_behaviour_from_v1_9_not_retained
Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => false) do
assert_raises(LocalJumpError) { invoke(new_expectation.yields(:foo)) }
end
assert_raises(LocalJumpError) { invoke(new_expectation.yields(:foo)) }
end

def test_yields_should_display_warning_when_caller_does_not_provide_block
DeprecationDisabler.disable_deprecations do
invoke(new_expectation.yields(:foo, 1, [2, 3]))
Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do
DeprecationDisabler.disable_deprecations do
invoke(new_expectation.yields(:foo, 1, [2, 3]))
end
end
assert message = Deprecation.messages.last
assert message.include?('Stubbed method was instructed to yield (:foo, 1, [2, 3])')
Expand All @@ -133,8 +133,10 @@ def test_yields_should_display_warning_when_caller_does_not_provide_block
end

def test_multiple_yields_should_display_warning_when_caller_does_not_provide_block
DeprecationDisabler.disable_deprecations do
invoke(new_expectation.multiple_yields(:foo, 1, [2, 3]))
Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do
DeprecationDisabler.disable_deprecations do
invoke(new_expectation.multiple_yields(:foo, 1, [2, 3]))
end
end
assert message = Deprecation.messages.last
assert message.include?('Stubbed method was instructed to yield (2, 3)')
Expand Down

0 comments on commit 6fcaf94

Please sign in to comment.