Skip to content

Commit e895734

Browse files
committed
feat: support response assertions
1 parent 2df4a5b commit e895734

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Master (Unreleased)
44

55
- Add `RSpecRails/HttpStatusNameConsistency` cop. ([@taketo1113])
6+
- Support correcting `assert_response` assertion in in `RSpec/Rails/MinitestAssertions`. ([@nzlaura])
67

78
## 2.31.0 (2025-03-10)
89

lib/rubocop/cop/rspec_rails/minitest_assertions.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,26 @@ def assertion
315315
end
316316
end
317317

318+
# :nodoc:
319+
class ResponseAssertion < BasicAssertion
320+
MATCHERS = %i[
321+
assert_response
322+
].freeze
323+
324+
# @!method self.minitest_assertion(node)
325+
def_node_matcher 'self.minitest_assertion', <<~PATTERN # rubocop:disable InternalAffairs/NodeMatcherDirective
326+
(send nil? :assert_response $_ $_?)
327+
PATTERN
328+
329+
def self.match(expected, failure_message)
330+
new(expected, nil, failure_message.first)
331+
end
332+
333+
def assertion
334+
"have_http_status(#{expected.source})"
335+
end
336+
end
337+
318338
MSG = 'Use `%<prefer>s`.'
319339

320340
# TODO: replace with `BasicAssertion.subclasses` in Ruby 3.1+

spec/rubocop/cop/rspec_rails/minitest_assertions_spec.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,4 +947,79 @@
947947
RUBY
948948
end
949949
end
950+
951+
context 'with response assertions' do
952+
it 'registers an offense when using `assert_response`' do
953+
expect_offense(<<~RUBY)
954+
assert_response :ok
955+
^^^^^^^^^^^^^^^^^^^ Use `expect(response).to have_http_status(:ok)`.
956+
RUBY
957+
958+
expect_correction(<<~RUBY)
959+
expect(response).to have_http_status(:ok)
960+
RUBY
961+
end
962+
963+
it 'registers an offense when using `assert_response` with parentheses' do
964+
expect_offense(<<~RUBY)
965+
assert_response(:ok)
966+
^^^^^^^^^^^^^^^^^^^^ Use `expect(response).to have_http_status(:ok)`.
967+
RUBY
968+
969+
expect_correction(<<~RUBY)
970+
expect(response).to have_http_status(:ok)
971+
RUBY
972+
end
973+
974+
it 'registers an offense when using `assert_response` with ' \
975+
'failure message' do
976+
expect_offense(<<~RUBY)
977+
assert_response :ok, "expected OK status"
978+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(response).to(have_http_status(:ok), "expected OK status")`.
979+
RUBY
980+
981+
expect_correction(<<~RUBY)
982+
expect(response).to(have_http_status(:ok), "expected OK status")
983+
RUBY
984+
end
985+
986+
it 'registers an offense when using `assert_response` with ' \
987+
'multi-line arguments' do
988+
expect_offense(<<~RUBY)
989+
assert_response(:ok,
990+
^^^^^^^^^^^^^^^^^^^^ Use `expect(response).to(have_http_status(:ok), "expected OK status")`.
991+
"expected OK status")
992+
RUBY
993+
994+
expect_correction(<<~RUBY)
995+
expect(response).to(have_http_status(:ok), "expected OK status")
996+
RUBY
997+
end
998+
999+
it 'registers an offense when using `assert_response` with ' \
1000+
'numeric status' do
1001+
expect_offense(<<~RUBY)
1002+
assert_response 200
1003+
^^^^^^^^^^^^^^^^^^^ Use `expect(response).to have_http_status(200)`.
1004+
RUBY
1005+
1006+
expect_correction(<<~RUBY)
1007+
expect(response).to have_http_status(200)
1008+
RUBY
1009+
end
1010+
1011+
it 'does not register an offense when using ' \
1012+
'`expect(response).to have_http_status`' do
1013+
expect_no_offenses(<<~RUBY)
1014+
expect(response).to have_http_status(:ok)
1015+
RUBY
1016+
end
1017+
1018+
it 'does not register an offense when using ' \
1019+
'`expect(response).to have_http_status` with numeric status' do
1020+
expect_no_offenses(<<~RUBY)
1021+
expect(response).to have_http_status(200)
1022+
RUBY
1023+
end
1024+
end
9501025
end

0 commit comments

Comments
 (0)