Skip to content

Commit 62d77f8

Browse files
authored
Merge pull request #76 from nzlaura/fix/add-assert-response
feat: support response assertions
2 parents 2df4a5b + fc0cfc1 commit 62d77f8

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 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

@@ -84,6 +85,7 @@
8485
[@jojos003]: https://github.com/jojos003
8586
[@mothonmars]: https://github.com/MothOnMars
8687
[@mvz]: https://github.com/mvz
88+
[@nzlaura]: https://github.com/nzlaura
8789
[@paydaylight]: https://github.com/paydaylight
8890
[@pirj]: https://github.com/pirj
8991
[@r7kamura]: https://github.com/r7kamura

docs/modules/ROOT/pages/cops_rspecrails.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ assert_nil a
364364
refute_empty(b)
365365
assert_true(a)
366366
assert_false(a)
367+
assert_response :ok
367368
368369
# good
369370
expect(b).to eq(a)
@@ -374,6 +375,7 @@ expect(a).to eq(nil)
374375
expect(a).not_to be_empty
375376
expect(a).to be(true)
376377
expect(a).to be(false)
378+
expect(response).to have_http_status(:ok)
377379
----
378380
379381
[#references-rspecrailsminitestassertions]

lib/rubocop/cop/rspec_rails/minitest_assertions.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module RSpecRails
1818
# refute_empty(b)
1919
# assert_true(a)
2020
# assert_false(a)
21+
# assert_response :ok
2122
#
2223
# # good
2324
# expect(b).to eq(a)
@@ -28,6 +29,7 @@ module RSpecRails
2829
# expect(a).not_to be_empty
2930
# expect(a).to be(true)
3031
# expect(a).to be(false)
32+
# expect(response).to have_http_status(:ok)
3133
#
3234
class MinitestAssertions < ::RuboCop::Cop::Base
3335
extend AutoCorrector
@@ -315,6 +317,28 @@ def assertion
315317
end
316318
end
317319

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

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

spec/rubocop/cop/rspec_rails/minitest_assertions_spec.rb

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,4 +947,90 @@
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 :redirect
955+
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(response).to have_http_status(:redirect)`.
956+
RUBY
957+
958+
expect_correction(<<~RUBY)
959+
expect(response).to have_http_status(:redirect)
960+
RUBY
961+
end
962+
963+
it 'registers an offense when using `assert_response` with a number' do
964+
expect_offense(<<~RUBY)
965+
assert_response 302
966+
^^^^^^^^^^^^^^^^^^^ Use `expect(response).to have_http_status(302)`.
967+
RUBY
968+
969+
expect_correction(<<~RUBY)
970+
expect(response).to have_http_status(302)
971+
RUBY
972+
end
973+
974+
it 'registers an offense when using `assert_response` with parentheses' do
975+
expect_offense(<<~RUBY)
976+
assert_response(:success)
977+
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(response).to have_http_status(:success)`.
978+
RUBY
979+
980+
expect_correction(<<~RUBY)
981+
expect(response).to have_http_status(:success)
982+
RUBY
983+
end
984+
985+
it 'registers an offense when using `assert_response` with ' \
986+
'failure message' do
987+
expect_offense(<<~RUBY)
988+
assert_response :success, "expected success status"
989+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(response).to(have_http_status(:success), "expected success status")`.
990+
RUBY
991+
992+
expect_correction(<<~RUBY)
993+
expect(response).to(have_http_status(:success), "expected success status")
994+
RUBY
995+
end
996+
997+
it 'registers an offense when using `assert_response` with ' \
998+
'multi-line arguments' do
999+
expect_offense(<<~RUBY)
1000+
assert_response(:redirect,
1001+
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(response).to(have_http_status(:redirect), "expected redirect status")`.
1002+
"expected redirect status")
1003+
RUBY
1004+
1005+
expect_correction(<<~RUBY)
1006+
expect(response).to(have_http_status(:redirect), "expected redirect status")
1007+
RUBY
1008+
end
1009+
1010+
it 'registers an offense when using `assert_response` with ' \
1011+
'numeric status' do
1012+
expect_offense(<<~RUBY)
1013+
assert_response 200
1014+
^^^^^^^^^^^^^^^^^^^ Use `expect(response).to have_http_status(200)`.
1015+
RUBY
1016+
1017+
expect_correction(<<~RUBY)
1018+
expect(response).to have_http_status(200)
1019+
RUBY
1020+
end
1021+
1022+
it 'does not register an offense when using ' \
1023+
'`expect(response).to have_http_status`' do
1024+
expect_no_offenses(<<~RUBY)
1025+
expect(response).to have_http_status(:success)
1026+
RUBY
1027+
end
1028+
1029+
it 'does not register an offense when using ' \
1030+
'`expect(response).to have_http_status` with numeric status' do
1031+
expect_no_offenses(<<~RUBY)
1032+
expect(response).to have_http_status(200)
1033+
RUBY
1034+
end
1035+
end
9501036
end

0 commit comments

Comments
 (0)