Class: RuboCop::Cop::RSpecRails::HaveHttpStatus

Inherits:
Base
  • Object
show all
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.

Examples:

ResponseMethods: [‘response’, ‘last_response’] (default)

# bad
expect(response.status).to be(200)
expect(last_response.code).to eq("200")

# good
expect(response).to have_http_status(200)
expect(last_response).to have_http_status(200)

ResponseMethods: [‘foo_response’]

# bad
expect(foo_response.status).to be(200)

# good
expect(foo_response).to have_http_status(200)

# also good
expect(response).to have_http_status(200)
expect(last_response).to have_http_status(200)

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

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/)

    message = format(MSG, response: response_method.method_name,
                          to: to, status: status,
                          bad_code: node.source)
    add_offense(node, message: 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