Skip to content

Commit

Permalink
display thrown object
Browse files Browse the repository at this point in the history
  • Loading branch information
nitishr committed Nov 6, 2019
1 parent 9007a97 commit 4e32c0c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
6 changes: 6 additions & 0 deletions lib/mocha/invocation.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}"
Expand Down
3 changes: 2 additions & 1 deletion lib/mocha/thrower.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions lib/mocha/thrown_object.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,37 @@ 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: #<Mock:foo>.bar(any_parameters)',
'- allowed any number of times, invoked 3 times: #<Mock:foo>.bar(any_parameters)',
' - #<Mock:foo>.bar(1, 2) # => "f"',
' - #<Mock:foo>.bar(3, 4) # => raised StandardError'
' - #<Mock:foo>.bar(3, 4) # => raised StandardError',
' - #<Mock:foo>.bar(5, 6) # => threw (:tag, "value")'
)
end

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: #<Mock:foo>.bar(any_parameters)',
'- allowed any number of times, invoked 3 times: #<Mock:foo>.bar(any_parameters)',
' - #<Mock:foo>.bar(1, 2) # => "f" after yielding ("b", "c"), then ("d", "e")',
' - #<Mock:foo>.bar(3, 4) # => raised StandardError after yielding ("b", "c"), then ("d", "e")'
' - #<Mock:foo>.bar(3, 4) # => raised StandardError after yielding ("b", "c"), then ("d", "e")',
' - #<Mock:foo>.bar(5, 6) # => threw (:tag, "value") after yielding ("b", "c"), then ("d", "e")'
)
end

Expand Down

0 comments on commit 4e32c0c

Please sign in to comment.