Sha256: bb2713a6b8245e088e96604c62458fd06edb853132b24730b76cac0e77025feb

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 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 < Cop
        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, :expression, format(MSG, node_source: node.source))
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ezcater_rubocop-0.49.7 lib/rubocop/cop/ezcater/rspec_require_http_status_matcher.rb
ezcater_rubocop-0.49.7.rc3 lib/rubocop/cop/ezcater/rspec_require_http_status_matcher.rb