From bb4ccd7dd06abf35dbdf54f10e551bb01d43c0a5 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Wed, 25 Dec 2019 00:28:45 +0000 Subject: [PATCH] deprecate current method dispatch behavior(jmock1) Display deprecation warning when adding an expectation if the expectation list already contains any expectations. The intent is to change method dispatch behavior to that of jMock v2 as suggested in #173 in a major version release. --- lib/mocha/expectation_list.rb | 9 +++++++++ test/unit/expectation_list_test.rb | 14 ++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/mocha/expectation_list.rb b/lib/mocha/expectation_list.rb index f5366ec17..a1de9f3e7 100644 --- a/lib/mocha/expectation_list.rb +++ b/lib/mocha/expectation_list.rb @@ -1,3 +1,5 @@ +require 'mocha/deprecation' + module Mocha class ExpectationList def initialize(expectations = []) @@ -5,6 +7,13 @@ def initialize(expectations = []) end def add(expectation) + if @expectations.any? + Deprecation.warning( + 'Expectations are currently searched from newest to oldest to find one that matches the invocation.', + ' This search order will be reversed in the future, such that expectations are searched from oldest to newest to find one that matches the invocation.', + ' This means you will have to reverse the order of `expects` and `stubs` calls on the same object if you want to retain the current behavior.' + ) + end @expectations.unshift(expectation) expectation end diff --git a/test/unit/expectation_list_test.rb b/test/unit/expectation_list_test.rb index 20a0de334..a2e1ae1ed 100644 --- a/test/unit/expectation_list_test.rb +++ b/test/unit/expectation_list_test.rb @@ -79,4 +79,18 @@ def test_should_combine_two_expectation_lists_into_one expectation_list = expectation_list1 + expectation_list2 assert_equal [expectation1, expectation2], expectation_list.to_a end + + def test_should_display_deprecation_warning_when_multiple_expectations_are_added + DeprecationDisabler.disable_deprecations do + expectation_list = ExpectationList.new + expectation1 = Expectation.new(nil, :my_method).with(:argument1, :argument2) + expectation2 = Expectation.new(nil, :my_method).with(:argument3, :argument4) + expectation_list.add(expectation1) + expectation_list.add(expectation2) + end + assert message = Deprecation.messages.last + assert message.include?('Expectations are currently searched from newest to oldest to find one that matches the invocation.') + assert message.include?('This search order will be reversed in the future, such that expectations are searched from oldest to newest to find one that matches the invocation.') + assert message.include?('This means you will have to reverse the order of `expects` and `stubs` calls on the same object if you want to retain the current behavior.') + end end