From 6ef520222d06f5988a415bc3bc9b9c214524e622 Mon Sep 17 00:00:00 2001 From: Nitish Rathi Date: Thu, 24 Oct 2019 19:27:58 +0100 Subject: [PATCH] DRY up acceptance test for display matching invocations Extract the assert_invocations method. Checking that invocations follow 'satisfied expectations:' ensures the invocations are displayed in the correct position within the other lines. --- ...invocations_alongside_expectations_test.rb | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/test/acceptance/display_matching_invocations_alongside_expectations_test.rb b/test/acceptance/display_matching_invocations_alongside_expectations_test.rb index 82402e687..236da47ed 100644 --- a/test/acceptance/display_matching_invocations_alongside_expectations_test.rb +++ b/test/acceptance/display_matching_invocations_alongside_expectations_test.rb @@ -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: #.bar(1)', - 'satisfied expectations:', + assert_invocations( + test_result, '- allowed any number of times, invoked twice: #.bar(any_parameters)', ' - #.bar(1, 2) # => "f"', ' - #.bar(3, 4) # => raised StandardError' - ], test_result.failure_message_lines + ) end def test_should_display_yields @@ -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: #.bar(1)', - 'satisfied expectations:', + assert_invocations( + test_result, '- allowed any number of times, invoked twice: #.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")' - ], test_result.failure_message_lines + ) end def test_should_display_empty_yield_and_return @@ -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: #.bar(1)', - 'satisfied expectations:', + assert_invocations( + test_result, '- allowed any number of times, invoked once: #.bar(any_parameters)', ' - #.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