Class: RuboCop::Cop::RSpecRails::HaveHttpStatus
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::RSpecRails::HaveHttpStatus
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/rspec_rails/have_http_status.rb
Overview
Checks that tests use ‘have_http_status` instead of equality matchers.
Constant Summary collapse
- MSG =
'Prefer `expect(%<response>s).%<to>s ' \ 'have_http_status(%<status>s)` over `%<bad_code>s`.'
- RUNNERS =
%i[to to_not not_to].to_set
- RESTRICT_ON_SEND =
RUNNERS
Instance Method Summary collapse
- #match_status(node) ⇒ Object
-
#on_send(node) ⇒ Object
rubocop:disable Metrics/MethodLength.
Instance Method Details
#match_status(node) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/rubocop/cop/rspec_rails/have_http_status.rb', line 39 def_node_matcher :match_status, <<~PATTERN (send (send nil? :expect $(send $(send nil? #response_methods?) {:status :code}) ) $RUNNERS $(send nil? {:be :eq :eql :equal} ({int str} $_)) ) PATTERN |
#on_send(node) ⇒ Object
rubocop:disable Metrics/MethodLength
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rubocop/cop/rspec_rails/have_http_status.rb', line 49 def on_send(node) # rubocop:disable Metrics/MethodLength match_status(node) do |response_status, response_method, to, match, status| return unless status.to_s.match?(/\A\d+\z/) = format(MSG, response: response_method.method_name, to: to, status: status, bad_code: node.source) add_offense(node, message: ) do |corrector| corrector.replace(response_status, response_method.method_name) corrector.replace(match.loc.selector, 'have_http_status') corrector.replace(match.first_argument, status.to_s) end end end |