#teardown
is probably a better way of achieving the desired outcome
MiniTest allows ensure
blocks, even with Rails syntax 🎉
I already knew Ruby methods (and classes) could have ensure blocks:
def some_method
do_something_that_might_blow_up
ensure
tidy_up_regardless
end
…and I knew MiniTest tests are just standard ruby methods:
def test_some_functionality
assert true
end
…but I wasn’t sure if the two worked together using Rails’ test
macro that lets you write nicer test names:
test "the truth" do
assert true
end
After spending some too much time trying to find an answer I eventually realised I should just try it out and see:
test 'the truth' do
assert true
raise StandardError
ensure
refute false
puts 'Clean up!'
end
And it worked! I see ‘Clean up!’ in the console and both assertions get called (in addition to the error being raised):
1 tests, 2 assertions, 0 failures, 1 errors, 0 skips