Enable `Minitest/AssertRaisesWithRegexpArgument` cop

This PR enables `Minitest/AssertRaisesWithRegexpArgument` cop
and it suppresses the new warning below.

```console
% bundle exec rubocop
(snip)

Offenses:

activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb:111:9: C: Minitest/AssertRaisesWithRegexpArgument:
Do not pass regular expression literals to assert_raises. Test the resulting exception.
assert_raises(ActiveRecord::StatementInvalid, /TypeError/) do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb:628:9: C: Minitest/AssertRaisesWithRegexpArgument:
Do not pass regular expression literals to assert_raises. Test the resulting exception.
assert_raises(ActiveRecord::StatementInvalid, /SQLite3::ReadOnlyException/) do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
railties/test/application/rake/multi_dbs_test.rb:307:13: C: Minitest/AssertRaisesWithRegexpArgument:
Do not pass regular expression literals to assert_raises. Test the resulting exception.
assert_raises RuntimeError, /You're using a multiple database application/ do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
railties/test/application/rake/multi_dbs_test.rb:311:13: C: Minitest/AssertRaisesWithRegexpArgument:
Do not pass regular expression literals to assert_raises. Test the resulting exception.
assert_raises RuntimeError, /You're using a multiple database application/ do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
railties/test/application/rake/multi_dbs_test.rb:336:13: C: Minitest/AssertRaisesWithRegexpArgument:
Do not pass regular expression literals to assert_raises. Test the resulting exception.
assert_raises RuntimeError, /You're using a multiple database application/ do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
railties/test/application/rake/multi_dbs_test.rb:361:13: C: Minitest/AssertRaisesWithRegexpArgument:
Do not pass regular expression literals to assert_raises. Test the resulting exception.
assert_raises RuntimeError, /You're using a multiple database application/ do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```

The last argument of `assert_raises` is a custom message string to help explain failures.
So, it's not the argument that `exception.message` is compared to.
`assert_raises` returns a raised exception and can be used to match against a regular expression.

And it updates the dependency version of rubocop-minitest in the Gemfile.
Because `Minitest/AssertRaisesWithRegexpArgument` cop was introduced in minitest 0.22.
https://github.com/rubocop/rubocop-minitest/releases/tag/v0.22.0
This commit is contained in:
Koichi ITO 2022-09-03 23:05:17 +09:00
parent 86a280a347
commit eeeb0b1b7a
3 changed files with 13 additions and 7 deletions

View File

@ -435,7 +435,7 @@ GEM
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.16.0)
parser (>= 3.1.1.0)
rubocop-minitest (0.17.0)
rubocop-minitest (0.22.0)
rubocop (>= 0.90, < 2.0)
rubocop-packaging (0.5.1)
rubocop (>= 0.89, < 2.0)

View File

@ -108,11 +108,12 @@ module ActiveRecord
end
def test_bad_timeout
assert_raises(ActiveRecord::StatementInvalid, /TypeError/) do
exception = assert_raises(ActiveRecord::StatementInvalid) do
Base.sqlite3_connection(database: ":memory:",
adapter: "sqlite3",
timeout: "usa").connect!
end
assert_match("TypeError", exception.message)
end
# connection is OK with a nil timeout
@ -625,9 +626,10 @@ module ActiveRecord
readonly: true
conn.connect!
assert_raises(ActiveRecord::StatementInvalid, /SQLite3::ReadOnlyException/) do
exception = assert_raises(ActiveRecord::StatementInvalid) do
conn.execute("CREATE TABLE test(id integer)")
end
assert_match("SQLite3::ReadOnlyException", exception.message)
end
def test_strict_strings_by_default

View File

@ -304,13 +304,15 @@ module ApplicationTests
down_output = rails("db:migrate:down:#{namespace}", "VERSION=#{version}")
up_output = rails("db:migrate:up:#{namespace}", "VERSION=#{version}")
else
assert_raises RuntimeError, /You're using a multiple database application/ do
exception = assert_raises RuntimeError do
down_output = rails("db:migrate:down", "VERSION=#{version}")
end
assert_match("You're using a multiple database application", exception.message)
assert_raises RuntimeError, /You're using a multiple database application/ do
exception = assert_raises RuntimeError do
up_output = rails("db:migrate:up", "VERSION=#{version}")
end
assert_match("You're using a multiple database application", exception.message)
end
case namespace
@ -333,9 +335,10 @@ module ApplicationTests
if namespace
rollback_output = rails("db:rollback:#{namespace}")
else
assert_raises RuntimeError, /You're using a multiple database application/ do
exception = assert_raises RuntimeError do
rollback_output = rails("db:rollback")
end
assert_match("You're using a multiple database application", exception.message)
end
case namespace
@ -358,9 +361,10 @@ module ApplicationTests
if namespace
redo_output = rails("db:migrate:redo:#{namespace}")
else
assert_raises RuntimeError, /You're using a multiple database application/ do
exception = assert_raises RuntimeError do
redo_output = rails("db:migrate:redo")
end
assert_match("You're using a multiple database application", exception.message)
end
case namespace