diff --git a/lib/mocha/mockery.rb b/lib/mocha/mockery.rb index 748f45a56..64799c7d9 100644 --- a/lib/mocha/mockery.rb +++ b/lib/mocha/mockery.rb @@ -35,12 +35,13 @@ def raise_not_initialized_error class << self def instance - instances.last || Null.new + @instances.last || Null.new end def setup + @instances ||= [] mockery = new - mockery.logger = instance.logger unless instances.empty? + mockery.logger = instance.logger unless @instances.empty? @instances.push(mockery) end @@ -52,13 +53,6 @@ def teardown instance.teardown ensure @instances.pop - @instances = nil if instances.empty? - end - - private - - def instances - @instances ||= [] end end @@ -92,11 +86,9 @@ def verify(assertion_counter = nil) end raise ExpectationErrorFactory.build(message, backtrace) end - expectations.each do |e| - unless Mocha.configuration.stubbing_method_unnecessarily == :allow - next if e.used? - on_stubbing_method_unnecessarily(e) - end + expectations.reject(&:used?).each do |expectation| + signature_proc = lambda { expectation.method_signature } + check(:stubbing_method_unnecessarily, 'method unnecessarily', signature_proc, expectation.backtrace) end end @@ -120,9 +112,9 @@ def state_machines def mocha_inspect message = '' - message << "unsatisfied expectations:\n- #{unsatisfied_expectations.map(&:mocha_inspect).join("\n- ")}\n" unless unsatisfied_expectations.empty? - message << "satisfied expectations:\n- #{satisfied_expectations.map(&:mocha_inspect).join("\n- ")}\n" unless satisfied_expectations.empty? - message << "states:\n- #{state_machines.map(&:mocha_inspect).join("\n- ")}" unless state_machines.empty? + message << "unsatisfied expectations:\n- #{unsatisfied_expectations.map(&:mocha_inspect).join("\n- ")}\n" if unsatisfied_expectations.any? + message << "satisfied expectations:\n- #{satisfied_expectations.map(&:mocha_inspect).join("\n- ")}\n" if satisfied_expectations.any? + message << "states:\n- #{state_machines.map(&:mocha_inspect).join("\n- ")}\n" if state_machines.any? message end @@ -139,11 +131,6 @@ def on_stubbing(object, method) check(:stubbing_method_on_non_mock_object, 'method on non-mock object', signature_proc) end - def on_stubbing_method_unnecessarily(expectation) - signature_proc = lambda { expectation.method_signature } - check(:stubbing_method_unnecessarily, 'method unnecessarily', signature_proc, expectation.backtrace) - end - attr_writer :logger def logger diff --git a/test/acceptance/multiple_expectations_failure_message_test.rb b/test/acceptance/multiple_expectations_failure_message_test.rb index d25ec721e..9ed2ca7e2 100644 --- a/test/acceptance/multiple_expectations_failure_message_test.rb +++ b/test/acceptance/multiple_expectations_failure_message_test.rb @@ -63,4 +63,20 @@ def test_should_report_multiple_satisfied_expectations '- expected exactly once, invoked once: #.method_one(any_parameters)' ], test_result.failure_message_lines end + + def test_should_include_state_in_unsatisfied_expectation_message + test_result = run_as_test do + mock = mock('mock') + readiness = states('readiness') + mock.expects(:method_one).once.then(readiness.is('ready')) + end + assert_failed(test_result) + assert_equal [ + 'not all expectations were satisfied', + 'unsatisfied expectations:', + '- expected exactly once, invoked never: #.method_one(any_parameters)', + 'states:', + '- readiness has no current state' + ], test_result.failure_message_lines + end end