rubocop / rubocop-minitest

Code style checking for Minitest files.
https://docs.rubocop.org/rubocop-minitest
MIT License
144 stars 44 forks source link

[Bug] Minitest/Assert* having conflicts with Rails/RefuteMethods policy #319

Open bogdan opened 3 months ago

bogdan commented 3 months ago

Bug Description

The rules Minitest/Assert* has some conflicts with Rails/RefuteMethods. It has default policy to convert refute_* statements to assert_not_* which causes Minitest/Assert* to be ignored.

Example:

# Statement
refute user.new_record?
# Will be converted by Rails/RefuteMethods to
assert_not user.new_record? 
# And will be ignored by Minitest/AssertPredicate

Fix

Make all rules consistent when converting generic refute or assert_not statements to refute_* or assert_not_* statements making sure it supports default policy established by Rails/RefuteMethods:

# bad
assert_not(obj.one?)
assert_not(obj.one?, 'message')

# good
assert_not_predicate(obj, :one?)
assert_not_predicate(obj, :one?, 'message')
Earlopain commented 3 months ago

assert_not_predicate is rails specific, I have a PR for rubocop-rails to handle this: https://github.com/rubocop/rubocop-rails/pull/1259. Also see https://github.com/rubocop/rubocop-rails/issues/1155

bogdan commented 3 months ago

Will it fix others like assert_not array.include?(value)?

Earlopain commented 3 months ago

Into what would this correct? I don't believe these methods work with arguments like that.

Earlopain commented 3 months ago

Sorry, my bad. No, this will not be handled by that PR. I can look into that once the other PR gets merged.

bogdan commented 3 months ago

Hm, you are right assert_not_include doesn't exist (which looks like a bug to me, but unrelated to rubocop in any wway). But assert_not_equal, assert_not_nil and many others do exist. The full list is here: https://api.rubyonrails.org/classes/ActiveSupport/TestCase.html#method-i-assert_no_match

Earlopain commented 3 months ago

refute array.include?(value) corrects into assert_not_includes array, value. Just one of those ruby File.exist?/File.exists? things I'd guess. Still, this is rails specific as well. Would you mind opening an issue in https://github.com/rubocop/rubocop-rails for that?