diff --git a/lib/mocha/invocation.rb b/lib/mocha/invocation.rb index 2cd19ab57..84d86c006 100644 --- a/lib/mocha/invocation.rb +++ b/lib/mocha/invocation.rb @@ -1,6 +1,7 @@ require 'mocha/parameters_matcher' require 'mocha/raised_exception' require 'mocha/return_values' +require 'mocha/thrown_object' require 'mocha/yield_parameters' module Mocha @@ -34,6 +35,11 @@ def raised(exception) @result = RaisedException.new(exception) end + # @private + def threw(tag, value) + @result = ThrownObject.new(tag, value) + end + # @private def mocha_inspect desc = "\n - #{@method_name}#{@arguments.mocha_inspect} # => #{@result.mocha_inspect}" diff --git a/lib/mocha/thrower.rb b/lib/mocha/thrower.rb index 0198e0c51..f3e4bc710 100644 --- a/lib/mocha/thrower.rb +++ b/lib/mocha/thrower.rb @@ -5,7 +5,8 @@ def initialize(tag, object = nil) @object = object end - def evaluate(_invocation) + def evaluate(invocation) + invocation.threw(@tag, @object) throw @tag, @object end end diff --git a/lib/mocha/thrown_object.rb b/lib/mocha/thrown_object.rb new file mode 100644 index 000000000..254b3dedf --- /dev/null +++ b/lib/mocha/thrown_object.rb @@ -0,0 +1,12 @@ +module Mocha + class ThrownObject + def initialize(tag, value = nil) + @tag = tag + @value = value + end + + def mocha_inspect + "threw (#{@tag.mocha_inspect}, #{@value.mocha_inspect})" + end + end +end diff --git a/test/acceptance/display_matching_invocations_alongside_expectations_test.rb b/test/acceptance/display_matching_invocations_alongside_expectations_test.rb index fd0676a94..4075a997b 100644 --- a/test/acceptance/display_matching_invocations_alongside_expectations_test.rb +++ b/test/acceptance/display_matching_invocations_alongside_expectations_test.rb @@ -19,16 +19,18 @@ def test_should_display_results test_result = run_as_test do foo = mock('foo') foo.expects(:bar).with(1).returns('a') - foo.stubs(:bar).with(any_parameters).returns('f').raises(StandardError) + foo.stubs(:bar).with(any_parameters).returns('f').raises(StandardError).throws(:tag, 'value') foo.bar(1, 2) { |_ignored| } assert_raise(StandardError) { foo.bar(3, 4) { |_ignored| } } + assert_throws(:tag) { foo.bar(5, 6) { |_ignored| } } end assert_invocations( test_result, - '- allowed any number of times, invoked twice: #.bar(any_parameters)', + '- allowed any number of times, invoked 3 times: #.bar(any_parameters)', ' - #.bar(1, 2) # => "f"', - ' - #.bar(3, 4) # => raised StandardError' + ' - #.bar(3, 4) # => raised StandardError', + ' - #.bar(5, 6) # => threw (:tag, "value")' ) end @@ -36,16 +38,18 @@ def test_should_display_yields test_result = run_as_test do foo = mock('foo') foo.expects(:bar).with(1).returns('a') - foo.stubs(:bar).with(any_parameters).multiple_yields(%w[b c], %w[d e]).returns('f').raises(StandardError) + foo.stubs(:bar).with(any_parameters).multiple_yields(%w[b c], %w[d e]).returns('f').raises(StandardError).throws(:tag, 'value') foo.bar(1, 2) { |_ignored| } assert_raise(StandardError) { foo.bar(3, 4) { |_ignored| } } + assert_throws(:tag) { foo.bar(5, 6) { |_ignored| } } end assert_invocations( test_result, - '- allowed any number of times, invoked twice: #.bar(any_parameters)', + '- allowed any number of times, invoked 3 times: #.bar(any_parameters)', ' - #.bar(1, 2) # => "f" after yielding ("b", "c"), then ("d", "e")', - ' - #.bar(3, 4) # => raised StandardError after yielding ("b", "c"), then ("d", "e")' + ' - #.bar(3, 4) # => raised StandardError after yielding ("b", "c"), then ("d", "e")', + ' - #.bar(5, 6) # => threw (:tag, "value") after yielding ("b", "c"), then ("d", "e")' ) end