Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Master (Unreleased)

- Fix a false positive for `RspecRails/NegationBeValid` when use `to_not`. ([@ydah])
- Supporting correcting `assest_redirected_to` in `RSpec/Rails/MinitestAssertions`. ([@nzlaura])

## 2.32.0 (2025-11-12)

Expand Down
2 changes: 2 additions & 0 deletions docs/modules/ROOT/pages/cops_rspecrails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ refute_empty(b)
assert_true(a)
assert_false(a)
assert_response :ok
assert_redirected_to '/users'

# good
expect(b).to eq(a)
Expand All @@ -376,6 +377,7 @@ expect(a).not_to be_empty
expect(a).to be(true)
expect(a).to be(false)
expect(response).to have_http_status(:ok)
expect(response).to redirect_to('/users')
----

[#references-rspecrailsminitestassertions]
Expand Down
24 changes: 24 additions & 0 deletions lib/rubocop/cop/rspec_rails/minitest_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module RSpecRails
# assert_true(a)
# assert_false(a)
# assert_response :ok
# assert_redirected_to '/users'
#
# # good
# expect(b).to eq(a)
Expand All @@ -30,6 +31,7 @@ module RSpecRails
# expect(a).to be(true)
# expect(a).to be(false)
# expect(response).to have_http_status(:ok)
# expect(response).to redirect_to('/users')
#
class MinitestAssertions < ::RuboCop::Cop::Base
extend AutoCorrector
Expand Down Expand Up @@ -339,6 +341,28 @@ def assertion
end
end

# :nodoc:
class RedirectAssertion < BasicAssertion
MATCHERS = %i[
assert_redirected_to
].freeze

ASSERT_ACTUAL_NODE = Struct.new(:source).new('response')

# @!method self.minitest_assertion(node)
def_node_matcher 'self.minitest_assertion', <<~PATTERN # rubocop:disable InternalAffairs/NodeMatcherDirective
(send nil? {:assert_redirected_to} $_ $_?)
PATTERN

def self.match(expected, failure_message)
new(expected, ASSERT_ACTUAL_NODE, failure_message.first)
end

def assertion
"redirect_to(#{expected})"
end
end

MSG = 'Use `%<prefer>s`.'

# TODO: replace with `BasicAssertion.subclasses` in Ruby 3.1+
Expand Down
101 changes: 101 additions & 0 deletions spec/rubocop/cop/rspec_rails/minitest_assertions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1033,4 +1033,105 @@
RUBY
end
end

context 'with redirect assertions' do
it 'registers an offense when using `assert_redirected_to` with a path' do
expect_offense(<<~RUBY)
assert_redirected_to '/users'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(response).to redirect_to('/users')`.
RUBY

expect_correction(<<~RUBY)
expect(response).to redirect_to('/users')
RUBY
end

# rubocop:disable Layout/LineLength
it 'registers an offense when using `assert_redirected_to` with parentheses' do
# rubocop:enable Layout/LineLength
expect_offense(<<~RUBY)
assert_redirected_to('/users')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(response).to redirect_to('/users')`.
RUBY

expect_correction(<<~RUBY)
expect(response).to redirect_to('/users')
RUBY
end

it 'registers an offense when using `assert_redirected_to` with ' \
'controller and action' do
expect_offense(<<~RUBY)
assert_redirected_to controller: 'users', action: 'show'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(response).to redirect_to(controller: 'users', action: 'show')`.
RUBY

expect_correction(<<~RUBY)
expect(response).to redirect_to(controller: 'users', action: 'show')
RUBY
end

it 'registers an offense when using `assert_redirected_to` with ' \
'failure message' do
expect_offense(<<~RUBY)
assert_redirected_to '/users', "expected redirect to users"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(response).to(redirect_to('/users'), "expected redirect to users")`.
RUBY

expect_correction(<<~RUBY)
expect(response).to(redirect_to('/users'), "expected redirect to users")
RUBY
end

it 'registers an offense when using `assert_redirected_to` with ' \
'multi-line arguments' do
expect_offense(<<~RUBY)
assert_redirected_to('/users',
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(response).to(redirect_to('/users'), "expected redirect to users")`.
"expected redirect to users")
RUBY

expect_correction(<<~RUBY)
expect(response).to(redirect_to('/users'), "expected redirect to users")
RUBY
end

it 'registers an offense when using `assert_redirected_to` with ' \
'a URL' do
expect_offense(<<~RUBY)
assert_redirected_to 'http://example.com/users'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(response).to redirect_to('http://example.com/users')`.
RUBY

expect_correction(<<~RUBY)
expect(response).to redirect_to('http://example.com/users')
RUBY
end

it 'registers an offense when using `assert_redirected_to` with ' \
'a named route' do
expect_offense(<<~RUBY)
assert_redirected_to users_path
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(response).to redirect_to(users_path)`.
RUBY

expect_correction(<<~RUBY)
expect(response).to redirect_to(users_path)
RUBY
end

it 'does not register an offense when using ' \
'`expect(response).to redirect_to`' do
expect_no_offenses(<<~RUBY)
expect(response).to redirect_to('/users')
RUBY
end

it 'does not register an offense when using ' \
'`expect(response).to redirect_to` with controller and action' do
expect_no_offenses(<<~RUBY)
expect(response).to redirect_to(controller: 'users', action: 'show')
RUBY
end
end
end