Skip to content

Commit 9b2e3ab

Browse files
committed
Fix an error for RSpec/Rails/HaveHttpStatus with comparison with strings containing non-numeric characters
Fix: #1623
1 parent 5a32e18 commit 9b2e3ab

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
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
- Fix a false positive in `RSpec/IndexedLet` with suffixes after index-like numbers. ([@pirj])
6+
- Fix an error for `RSpec/Rails/HaveHttpStatus` with comparison with strings containing non-numeric characters. ([@ydah])
67

78
## 2.20.0 (2023-04-18)
89

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class HaveHttpStatus < ::RuboCop::Cop::Base
1818
extend AutoCorrector
1919

2020
MSG =
21-
'Prefer `expect(response).%<to>s have_http_status(%<status>i)` ' \
21+
'Prefer `expect(response).%<to>s have_http_status(%<status>s)` ' \
2222
'over `%<bad_code>s`.'
2323

2424
RUNNERS = %i[to to_not not_to].to_set
@@ -37,12 +37,14 @@ class HaveHttpStatus < ::RuboCop::Cop::Base
3737

3838
def on_send(node)
3939
match_status(node) do |response_status, to, match, status|
40+
return unless status.to_s.match?(/\A\d+\z/)
41+
4042
message = format(MSG, to: to, status: status,
4143
bad_code: node.source)
4244
add_offense(node, message: message) do |corrector|
4345
corrector.replace(response_status, 'response')
4446
corrector.replace(match.loc.selector, 'have_http_status')
45-
corrector.replace(match.first_argument, status.to_i.to_s)
47+
corrector.replace(match.first_argument, status.to_s)
4648
end
4749
end
4850
end

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,10 @@
4545
it { expect(res.status).to be(200) }
4646
RUBY
4747
end
48+
49+
it 'ignores statuses that would not coerce to an integer' do
50+
expect_no_offenses(<<~RUBY)
51+
it { expect(response.status).to eq("404 Not Found") }
52+
RUBY
53+
end
4854
end

0 commit comments

Comments
 (0)