-
-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new RSpec/Rails/HaveHttpStatus cop
- Loading branch information
Showing
8 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
module Rails | ||
# Checks that tests use `have_http_status` instead of equality matchers. | ||
# | ||
# @example | ||
# # bad | ||
# expect(response.status).to be(200) | ||
# | ||
# # good | ||
# expect(response).to have_http_status(200) | ||
# | ||
class HaveHttpStatus < Base | ||
extend AutoCorrector | ||
|
||
MSG = | ||
'Prefer `expect(response).%<to>s have_http_status(%<status>i)` ' \ | ||
'over `expect(response.status).%<to>s %<match>s`.' | ||
|
||
# @!method match_status(node) | ||
def_node_matcher :match_status, <<-PATTERN | ||
(send | ||
(send nil? :expect | ||
$(send (send nil? :response) :status) | ||
) | ||
$#Runners.all | ||
$(send nil? {:be :eq :eql :equal} (int $_)) | ||
) | ||
PATTERN | ||
|
||
def on_send(node) | ||
match_status(node) do |response_status, to, match, status| | ||
message = format(MSG, to: to, match: match.source, status: status) | ||
add_offense(node, message: message) do |corrector| | ||
corrector.replace(response_status.source_range, 'response') | ||
corrector.replace(match.loc.selector, 'have_http_status') | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::Rails::HaveHttpStatus do | ||
it 'registers an offense for `expect(response.status).to be(200)`' do | ||
expect_offense(<<~RUBY) | ||
it { expect(response.status).to be(200) } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `expect(response).to have_http_status(200)` over `expect(response.status).to be(200)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
it { expect(response).to have_http_status(200) } | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for `expect(response.status).not_to eq(404)`' do | ||
expect_offense(<<~RUBY) | ||
it { expect(response.status).not_to eq(404) } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `expect(response).not_to have_http_status(404)` over `expect(response.status).not_to eq(404)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
it { expect(response).not_to have_http_status(404) } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for `is_expected.to be(200)`' do | ||
expect_no_offenses(<<~RUBY) | ||
it { is_expected.to be(200) } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for `expect(res.status).to be(200)`' do | ||
expect_no_offenses(<<~RUBY) | ||
it { expect(res.status).to be(200) } | ||
RUBY | ||
end | ||
end |