Skip to content

Commit

Permalink
feat: support assert_not_equal and assert_not_nil in `RSpec/Rails…
Browse files Browse the repository at this point in the history
…/MinitestAssertions`
  • Loading branch information
G-Rath committed Jan 10, 2024
1 parent fd8c95e commit 095181d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -2,7 +2,8 @@

## Master (Unreleased)

- Support correcting `assert_nil` and `refute_nil` to `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
- Support correcting `assert_nil` and `refute_nil` in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
- Support correcting `assert_not_equal` and `assert_not_equal` in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
- Fix a false positive for `RSpec/ExpectActual` when used with rspec-rails routing matchers. ([@naveg])
- Add new `RSpec/RepeatedSubjectCall` cop. ([@drcapulet])

Expand Down
6 changes: 4 additions & 2 deletions lib/rubocop/cop/rspec/rails/minitest_assertions.rb
Expand Up @@ -29,19 +29,21 @@ class MinitestAssertions < Base
MSG = 'Use `%<prefer>s`.'
RESTRICT_ON_SEND = %i[
assert_equal
assert_not_equal
refute_equal
assert_nil
assert_not_nil
refute_nil
].freeze

# @!method minitest_equal_assertion(node)
def_node_matcher :minitest_equal_assertion, <<~PATTERN
(send nil? {:assert_equal :refute_equal} $_ $_ $_?)
(send nil? {:assert_equal :assert_not_equal :refute_equal} $_ $_ $_?)
PATTERN

# @!method minitest_nil_assertion(node)
def_node_matcher :minitest_nil_assertion, <<~PATTERN
(send nil? {:assert_nil :refute_nil} $_ $_?)
(send nil? {:assert_nil :assert_not_nil :refute_nil} $_ $_?)
PATTERN

def on_send(node)
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/rspec/rails/minitest_assertions_spec.rb
Expand Up @@ -49,6 +49,17 @@
RUBY
end

it 'registers an offense when using `assert_not_equal`' do
expect_offense(<<~RUBY)
assert_not_equal a, b
^^^^^^^^^^^^^^^^^^^^^ Use `expect(b).not_to eq(a)`.
RUBY

expect_correction(<<~RUBY)
expect(b).not_to eq(a)
RUBY
end

it 'registers an offense when using `refute_equal`' do
expect_offense(<<~RUBY)
refute_equal a, b
Expand Down Expand Up @@ -120,6 +131,17 @@
RUBY
end

it 'registers an offense when using `assert_not_nil`' do
expect_offense(<<~RUBY)
assert_not_nil a
^^^^^^^^^^^^^^^^ Use `expect(a).not_to eq(nil)`.
RUBY

expect_correction(<<~RUBY)
expect(a).not_to eq(nil)
RUBY
end

it 'registers an offense when using `refute_nil`' do
expect_offense(<<~RUBY)
refute_nil a
Expand Down

0 comments on commit 095181d

Please sign in to comment.