Reputation: 2402
When creating a helper method for unit testing, the console output of the failure only indicates the line where the actual assertion happened.
# Just an example method.
def test_equal(a, b)
assert_equal a, b # Error indicates this line
end
test_equal(1, 2) # I'd like to know this line
test_equal(2, 3)
Is there a way of showing something like a "backtrace" or provide more context for these assertion errors?
Upvotes: 4
Views: 385
Reputation: 2402
I've analyzed this matter further and stumbled across minitest's method Assertion.location. There, it goes through the backtrace (failed assertions result in exceptions of type Minitest::Assertion < Exception
), reverses it and looks for methods starting with one of the following keywords:
assert
refute
flunk
pass
fail
raise
must
wont
Therefore, the example in the original question should look like the following:
# Just an example method.
def assert_my_equal(a, b)
assert_equal a, b # Error indicates this line
end
assert_my_equal(1, 2) # This is the line number indicated in the failure report
assert_my_equal(2, 3)
This also explains why the Rails-specific assertion methods behave correctly: They start with assert_
.
Upvotes: 7