Skip to content

Commit

Permalink
DRY up acceptance test for display matching invocations
Browse files Browse the repository at this point in the history
Extract the assert_invocations method. Checking that invocations follow
'satisfied expectations:' ensures the invocations are displayed in the
correct position within the other lines.
  • Loading branch information
nitishr authored and floehopper committed Nov 8, 2019
1 parent c11c367 commit 6ef5202
Showing 1 changed file with 15 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,12 @@ def test_should_display_results
foo.bar(1, 2)
assert_raise(StandardError) { foo.bar(3, 4) }
end
assert_failed(test_result)
assert_equal [
'not all expectations were satisfied',
'unsatisfied expectations:',
'- expected exactly once, not yet invoked: #<Mock:foo>.bar(1)',
'satisfied expectations:',
assert_invocations(
test_result,
'- allowed any number of times, invoked twice: #<Mock:foo>.bar(any_parameters)',
' - #<Mock:foo>.bar(1, 2) # => "f"',
' - #<Mock:foo>.bar(3, 4) # => raised StandardError'
], test_result.failure_message_lines
)
end

def test_should_display_yields
Expand All @@ -45,16 +41,12 @@ def test_should_display_yields
foo.bar(1, 2) { |_ignored| }
assert_raise(StandardError) { foo.bar(3, 4) { |_ignored| } }
end
assert_failed(test_result)
assert_equal [
'not all expectations were satisfied',
'unsatisfied expectations:',
'- expected exactly once, not yet invoked: #<Mock:foo>.bar(1)',
'satisfied expectations:',
assert_invocations(
test_result,
'- allowed any number of times, invoked twice: #<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")'
], test_result.failure_message_lines
)
end

def test_should_display_empty_yield_and_return
Expand All @@ -65,14 +57,16 @@ def test_should_display_empty_yield_and_return

foo.bar(1, 2) { |_ignored| }
end
assert_failed(test_result)
assert_equal [
'not all expectations were satisfied',
'unsatisfied expectations:',
'- expected exactly once, not yet invoked: #<Mock:foo>.bar(1)',
'satisfied expectations:',
assert_invocations(
test_result,
'- allowed any number of times, invoked once: #<Mock:foo>.bar(any_parameters)',
' - #<Mock:foo>.bar(1, 2) # => nil after yielding ()'
], test_result.failure_message_lines
)
end

def assert_invocations(test_result, *invocations)
assert_failed(test_result)
assert_equal invocations.unshift('satisfied expectations:'),
test_result.failure_message_lines[-invocations.size..-1]
end
end

0 comments on commit 6ef5202

Please sign in to comment.