rubocop-minitest icon indicating copy to clipboard operation
rubocop-minitest copied to clipboard

New cop to check `assert_raises` with generic exceptions is followed by error message check

Open fatkodima opened this issue 2 years ago • 2 comments

When testing for generic errors (like ArgumentError, RuntimeError, KeyError, IndexError etc) we need to check for the error message to not get false positives.

def foo(a, b, c)
  raise ArgumentError, "c should be 'foo'" unless c == "foo"
  # ...
end

# bad
assert_raises(ArgumentError) do
  foo(1, 2) # c is missing
end

# good
error = assert_raises(ArgumentError) do
  foo(1, 2)
end

assert_equal "c should be 'foo'", error.message # or assert_match or assert_includes

assert_raises(RuntimeError) is a 💥

fatkodima avatar Jun 03 '22 13:06 fatkodima

@koic Wdyt on this?

fatkodima avatar Jul 26 '22 12:07 fatkodima

If it is disabled by default, I think it can be accepted. Probably because it is too strict as a default rule.

koic avatar Jul 28 '22 07:07 koic