Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support assert_not_equal and assert_not_nil in RSpec/Rails/MinitestAssertions #1776

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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