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