Skip to content

Commit cfa122e

Browse files
authored
Merge pull request #1610 from ydah/fix/1609
Add support for `RSpec/HaveHttpStatus` when using `response.code`
2 parents 80c7d96 + cde8a69 commit cfa122e

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Add autocorrect support for `RSpec/ScatteredSetup`. ([@ydah])
1313
- Fix a false negative for `RSpec/RedundantAround` when redundant numblock `around`. ([@ydah])
1414
- Add support for shared example groups to `RSpec/EmptyLineAfterExampleGroup`. ([@pirj])
15+
- Add support for `RSpec/HaveHttpStatus` when using `response.code`. ([@ydah])
1516

1617
## 2.19.0 (2023-03-06)
1718

docs/modules/ROOT/pages/cops_rspec_rails.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Checks that tests use `have_http_status` instead of equality matchers.
5353
----
5454
# bad
5555
expect(response.status).to be(200)
56+
expect(response.code).to eq("200")
5657
5758
# good
5859
expect(response).to have_http_status(200)

lib/rubocop/cop/rspec/rails/have_http_status.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Rails
99
# @example
1010
# # bad
1111
# expect(response.status).to be(200)
12+
# expect(response.code).to eq("200")
1213
#
1314
# # good
1415
# expect(response).to have_http_status(200)
@@ -18,7 +19,7 @@ class HaveHttpStatus < ::RuboCop::Cop::Base
1819

1920
MSG =
2021
'Prefer `expect(response).%<to>s have_http_status(%<status>i)` ' \
21-
'over `expect(response.status).%<to>s %<match>s`.'
22+
'over `%<bad_code>s`.'
2223

2324
RUNNERS = %i[to to_not not_to].to_set
2425
RESTRICT_ON_SEND = RUNNERS
@@ -27,19 +28,21 @@ class HaveHttpStatus < ::RuboCop::Cop::Base
2728
def_node_matcher :match_status, <<-PATTERN
2829
(send
2930
(send nil? :expect
30-
$(send (send nil? :response) :status)
31+
$(send (send nil? :response) {:status :code})
3132
)
3233
$RUNNERS
33-
$(send nil? {:be :eq :eql :equal} (int $_))
34+
$(send nil? {:be :eq :eql :equal} ({int str} $_))
3435
)
3536
PATTERN
3637

3738
def on_send(node)
3839
match_status(node) do |response_status, to, match, status|
39-
message = format(MSG, to: to, match: match.source, status: status)
40+
message = format(MSG, to: to, status: status,
41+
bad_code: node.source)
4042
add_offense(node, message: message) do |corrector|
4143
corrector.replace(response_status, 'response')
4244
corrector.replace(match.loc.selector, 'have_http_status')
45+
corrector.replace(match.first_argument, status.to_i.to_s)
4346
end
4447
end
4548
end

spec/rubocop/cop/rspec/rails/have_http_status_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@
2323
RUBY
2424
end
2525

26+
it 'registers an offense for `expect(response.code).to eq("200")`' do
27+
expect_offense(<<~RUBY)
28+
it { expect(response.code).to eq("200") }
29+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `expect(response).to have_http_status(200)` over `expect(response.code).to eq("200")`.
30+
RUBY
31+
32+
expect_correction(<<~RUBY)
33+
it { expect(response).to have_http_status(200) }
34+
RUBY
35+
end
36+
2637
it 'does not register an offense for `is_expected.to be(200)`' do
2738
expect_no_offenses(<<~RUBY)
2839
it { is_expected.to be(200) }

0 commit comments

Comments
 (0)