-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #207 from r7kamura/status
Add `RSpecRailsStatusCodeCheckBySubject` cop
- Loading branch information
Showing
6 changed files
with
105 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
52 changes: 52 additions & 0 deletions
52
lib/rubocop/cop/sevencop/rspec_rails_status_code_check_by_subject.rb
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,52 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Sevencop | ||
# Use `expect(response).to have_http_status(code)` instead of `is_expected.to eq(code)`. | ||
# | ||
# @example | ||
# # bad | ||
# is_expected.to eq(200) | ||
# | ||
# # good | ||
# expect(response).to have_http_status(200) | ||
# | ||
# # bad | ||
# is_expected.to eq(:ok) | ||
# | ||
# # good | ||
# expect(response).to have_http_status(:ok) | ||
class RSpecRailsStatusCodeCheckBySubject < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `expect(response).to have_http_status(code)` instead of `is_expected.to eq(code)`.' | ||
|
||
RESTRICT_ON_SEND = %i[ | ||
to | ||
].freeze | ||
|
||
# @param [RuboCop::AST::SendNode] node | ||
# @return [void] | ||
def on_send(node) | ||
return unless is_expected_to_eq_code?(node) | ||
|
||
add_offense(node) do |corrector| | ||
corrector.insert_before(node, "subject\n") | ||
corrector.replace(node.receiver, 'expect(response)') | ||
corrector.replace(node.first_argument.location.selector, 'have_http_status') | ||
end | ||
end | ||
|
||
private | ||
|
||
# @!method is_expected_to_eq_code?(node) | ||
# @param [RuboCop::AST::SendNode] node | ||
# @return [Boolean] | ||
def_node_matcher :is_expected_to_eq_code?, <<~PATTERN | ||
(send (send nil? :is_expected) :to (send nil? :eq {int sym})) | ||
PATTERN | ||
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
41 changes: 41 additions & 0 deletions
41
spec/rubocop/cop/sevencop/rspec_rails_status_code_check_by_subject_spec.rb
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,41 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe RuboCop::Cop::Sevencop::RSpecRailsStatusCodeCheckBySubject, :config do | ||
context 'when `is_expected.to redirect_to(root_path)` is used' do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
is_expected.to redirect_to(root_path) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when `is_expected.to eq(200)` is used' do | ||
it 'registers offense' do | ||
expect_offense(<<~RUBY) | ||
is_expected.to eq(200) | ||
^^^^^^^^^^^^^^^^^^^^^^ Use `expect(response).to have_http_status(code)` instead of `is_expected.to eq(code)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
subject | ||
expect(response).to have_http_status(200) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when `is_expected.to eq(:ok)` is used' do | ||
it 'registers offense' do | ||
expect_offense(<<~RUBY) | ||
is_expected.to eq(:ok) | ||
^^^^^^^^^^^^^^^^^^^^^^ Use `expect(response).to have_http_status(code)` instead of `is_expected.to eq(code)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
subject | ||
expect(response).to have_http_status(:ok) | ||
RUBY | ||
end | ||
end | ||
end |