Sha256: 39e9d9cc5fd15d518a240cd4d9cdb015b141af670967263a1b64bcd4485c0e1c

Contents?: true

Size: 1.23 KB

Versions: 6

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Ezcater
      # Enforce use of HTTP status code matchers rather than asserting on on random numbers.
      #
      # @example
      #
      #   # good
      #   expect(response).to have_http_status :created
      #   expect(response).to have_http_status :bad_request
      #
      #   # bad
      #   expect(response.code).to eq 201
      #   expect(response.code).to eq 400
      class RspecRequireHttpStatusMatcher < Base
        MSG = "Use the `have_http_status` matcher, like `expect(response).to have_http_status :bad_request`, "\
          "rather than `%<node_source>s`"

        def_node_matcher :response_status_assertion, <<~PATTERN
          (send (send _ :expect (send (send _ :response) :status)) :to (send _ :eq (int _)))
        PATTERN

        def_node_matcher :response_code_assertion, <<~PATTERN
          (send (send _ :expect (send (send _ :response) :code)) :to (send _ :eq (str _)))
        PATTERN

        def on_send(node)
          return if !response_status_assertion(node) && !response_code_assertion(node)

          add_offense(node.loc.expression,
                      message: format(MSG, node_source: node.source))
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ezcater_rubocop-9.0.0 lib/rubocop/cop/ezcater/rspec_require_http_status_matcher.rb
ezcater_rubocop-8.1.0 lib/rubocop/cop/ezcater/rspec_require_http_status_matcher.rb
ezcater_rubocop-8.0.0 lib/rubocop/cop/ezcater/rspec_require_http_status_matcher.rb
ezcater_rubocop-7.1.2 lib/rubocop/cop/ezcater/rspec_require_http_status_matcher.rb
ezcater_rubocop-7.1.1 lib/rubocop/cop/ezcater/rspec_require_http_status_matcher.rb
ezcater_rubocop-7.1.0 lib/rubocop/cop/ezcater/rspec_require_http_status_matcher.rb