Skip to content

Commit

Permalink
Improve test runner assertions - failure vs error
Browse files Browse the repository at this point in the history
Make more of a distinction between test failures and errors. Explicitly
assert for what we are expecting and flunk with an appropriate error
message if that's not what happened.
  • Loading branch information
floehopper committed Oct 12, 2022
1 parent 4f59e27 commit eec7200
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 32 deletions.
8 changes: 4 additions & 4 deletions test/acceptance/stubbing_error_backtrace_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_should_display_backtrace_indicating_line_number_where_attempt_to_stub_n
test_result = run_as_test do
execution_point = ExecutionPoint.current; object.stubs(:non_existent_method)
end
assert_equal 1, test_result.error_count
assert_errored(test_result)
assert_equal execution_point, ExecutionPoint.new(test_result.errors[0].exception.backtrace)
end

Expand All @@ -34,7 +34,7 @@ def non_public_method; end
test_result = run_as_test do
execution_point = ExecutionPoint.current; object.stubs(:non_public_method)
end
assert_equal 1, test_result.error_count
assert_errored(test_result)
assert_equal execution_point, ExecutionPoint.new(test_result.errors[0].exception.backtrace)
end

Expand All @@ -45,7 +45,7 @@ def test_should_display_backtrace_indicating_line_number_where_attempt_to_stub_m
test_result = run_as_test do
execution_point = ExecutionPoint.current; object.stubs(:any_method)
end
assert_equal 1, test_result.error_count
assert_errored(test_result)
assert_equal execution_point, ExecutionPoint.new(test_result.errors[0].exception.backtrace)
end

Expand All @@ -56,7 +56,7 @@ def test_should_display_backtrace_indicating_line_number_where_method_was_unnece
test_result = run_as_test do
execution_point = ExecutionPoint.current; object.stubs(:unused_method)
end
assert_equal 1, test_result.error_count
assert_errored(test_result)
assert_equal execution_point, ExecutionPoint.new(test_result.errors[0].exception.backtrace)
end
# rubocop:enable Style/Semicolon
Expand Down
18 changes: 6 additions & 12 deletions test/acceptance/stubbing_frozen_object_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ def test_should_fail_fast_if_attempting_to_stub_method_on_frozen_object
test_result = run_as_test do
execution_point = ExecutionPoint.current; object.stubs(:stubbed_method)
end
assert_failed(test_result)
assert_equal 1, test_result.error_count
assert_errored(test_result)
assert_equal execution_point, ExecutionPoint.new(test_result.errors[0].exception.backtrace)
end

Expand All @@ -32,8 +31,7 @@ def test_should_fail_fast_if_attempting_to_expect_method_on_frozen_object
test_result = run_as_test do
execution_point = ExecutionPoint.current; object.expects(:stubbed_method)
end
assert_failed(test_result)
assert_equal 1, test_result.error_count
assert_errored(test_result)
assert_equal execution_point, ExecutionPoint.new(test_result.errors[0].exception.backtrace)
end

Expand All @@ -44,8 +42,7 @@ def test_should_fail_fast_if_attempting_to_stub_method_on_frozen_class
test_result = run_as_test do
execution_point = ExecutionPoint.current; klass.stubs(:stubbed_method)
end
assert_failed(test_result)
assert_equal 1, test_result.error_count
assert_errored(test_result)
assert_equal execution_point, ExecutionPoint.new(test_result.errors[0].exception.backtrace)
end

Expand All @@ -56,8 +53,7 @@ def test_should_fail_fast_if_attempting_to_expect_method_on_frozen_class
test_result = run_as_test do
execution_point = ExecutionPoint.current; klass.expects(:stubbed_method)
end
assert_failed(test_result)
assert_equal 1, test_result.error_count
assert_errored(test_result)
assert_equal execution_point, ExecutionPoint.new(test_result.errors[0].exception.backtrace)
end

Expand All @@ -68,8 +64,7 @@ def test_should_fail_fast_if_attempting_to_stub_method_on_any_instance_of_frozen
test_result = run_as_test do
execution_point = ExecutionPoint.current; klass.any_instance.stubs(:stubbed_method)
end
assert_failed(test_result)
assert_equal 1, test_result.error_count
assert_errored(test_result)
assert_equal execution_point, ExecutionPoint.new(test_result.errors[0].exception.backtrace)
end

Expand All @@ -80,8 +75,7 @@ def test_should_fail_fast_if_attempting_to_expect_method_on_any_instance_of_froz
test_result = run_as_test do
execution_point = ExecutionPoint.current; klass.any_instance.expects(:stubbed_method)
end
assert_failed(test_result)
assert_equal 1, test_result.error_count
assert_errored(test_result)
assert_equal execution_point, ExecutionPoint.new(test_result.errors[0].exception.backtrace)
end
# rubocop:enable Style/Semicolon
Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/stubbing_method_unnecessarily_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_should_prevent_stubbing_method_unnecessarily
mock = mock('mock')
mock.stubs(:public_method)
end
assert_failed(test_result)
assert_errored(test_result)
assert test_result.error_messages.include?('Mocha::StubbingError: stubbing method unnecessarily: #<Mock:mock>.public_method(any_parameters)')
end

Expand Down
4 changes: 2 additions & 2 deletions test/acceptance/stubbing_nil_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ def test_should_prevent_stubbing_method_on_nil
test_result = run_as_test do
nil.stubs(:stubbed_method)
end
assert_failed(test_result)
assert_errored(test_result)
assert test_result.error_messages.include?('Mocha::StubbingError: stubbing method on nil: nil.stubbed_method')
end

def test_should_default_to_prevent_stubbing_method_on_non_mock_object
test_result = run_as_test do
nil.stubs(:stubbed_method)
end
assert_failed(test_result)
assert_errored(test_result)
assert test_result.error_messages.include?('Mocha::StubbingError: stubbing method on nil: nil.stubbed_method')
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_should_prevent_stubbing_non_existent_any_instance_method
test_result = run_as_test do
klass.any_instance.stubs(:non_existent_method)
end
assert_failed(test_result)
assert_errored(test_result)
assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-existent method: #{klass.any_instance.mocha_inspect}.non_existent_method")
end

Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/stubbing_non_existent_class_method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_should_prevent_stubbing_non_existent_class_method
test_result = run_as_test do
klass.stubs(:non_existent_method)
end
assert_failed(test_result)
assert_errored(test_result)
assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-existent method: #{klass.mocha_inspect}.non_existent_method")
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_should_prevent_stubbing_non_existent_instance_method
test_result = run_as_test do
instance.stubs(:non_existent_method)
end
assert_failed(test_result)
assert_errored(test_result)
assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-existent method: #{instance.mocha_inspect}.non_existent_method")
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def private_method; end
test_result = run_as_test do
klass.any_instance.stubs(:private_method)
end
assert_failed(test_result)
assert_errored(test_result)
assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-public method: #{klass.any_instance.mocha_inspect}.private_method")
end

Expand All @@ -86,7 +86,7 @@ def protected_method; end
test_result = run_as_test do
klass.any_instance.stubs(:protected_method)
end
assert_failed(test_result)
assert_errored(test_result)
assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-public method: #{klass.any_instance.mocha_inspect}.protected_method")
end

Expand Down
4 changes: 2 additions & 2 deletions test/acceptance/stubbing_non_public_class_method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def private_method; end
test_result = run_as_test do
klass.stubs(:private_method)
end
assert_failed(test_result)
assert_errored(test_result)
assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-public method: #{klass.mocha_inspect}.private_method")
end

Expand All @@ -99,7 +99,7 @@ def protected_method; end
test_result = run_as_test do
klass.stubs(:protected_method)
end
assert_failed(test_result)
assert_errored(test_result)
assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-public method: #{klass.mocha_inspect}.protected_method")
end

Expand Down
4 changes: 2 additions & 2 deletions test/acceptance/stubbing_non_public_instance_method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def private_method; end
test_result = run_as_test do
instance.stubs(:private_method)
end
assert_failed(test_result)
assert_errored(test_result)
assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-public method: #{instance.mocha_inspect}.private_method")
end

Expand All @@ -86,7 +86,7 @@ def protected_method; end
test_result = run_as_test do
instance.stubs(:protected_method)
end
assert_failed(test_result)
assert_errored(test_result)
assert test_result.error_messages.include?("Mocha::StubbingError: stubbing non-public method: #{instance.mocha_inspect}.protected_method")
end

Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/stubbing_on_non_mock_object_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def existing_method; end
test_result = run_as_test do
non_mock_object.stubs(:existing_method)
end
assert_failed(test_result)
assert_errored(test_result)
assert test_result.error_messages.include?("Mocha::StubbingError: stubbing method on non-mock object: #{non_mock_object.mocha_inspect}.existing_method")
end

Expand Down
2 changes: 1 addition & 1 deletion test/integration/shared_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def test_real_object_expectation_does_not_leak_into_subsequent_test
execution_point = ExecutionPoint.current; klass.foo
}
)
assert_failed(test_result)
assert_errored(test_result)
exception = test_result.errors.first.exception
assert_equal execution_point, ExecutionPoint.new(exception.backtrace)
assert_match(/undefined method `foo'/, exception.message)
Expand Down
9 changes: 7 additions & 2 deletions test/test_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,16 @@ def run_as_tests(methods = {})
# rubocop:enable Metrics/AbcSize

def assert_passed(test_result)
flunk "Test errored unexpectedly with message: #{test_result.errors.map(&:exception)}" if test_result.error_count > 0
flunk "Test failed unexpectedly with message: #{test_result.failures}" if test_result.failure_count > 0
flunk "Test failed unexpectedly with message: #{test_result.errors.map(&:exception)}" if test_result.error_count > 0
end

def assert_failed(test_result)
flunk 'Test passed unexpectedly' unless test_result.failure_count + test_result.error_count > 0
flunk "Test errored unexpectedly with message: #{test_result.errors.map(&:exception)}" if test_result.error_count > 0
flunk 'Test passed unexpectedly' unless test_result.failure_count > 0
end

def assert_errored(test_result)
flunk 'Test did not error as expected' unless test_result.error_count > 0
end
end

0 comments on commit eec7200

Please sign in to comment.