Sha256: 1acfbd8bead900f953ba63128cbaac48a699969a07189bb44d470c9602f8c3a6

Contents?: true

Size: 1.65 KB

Versions: 13

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      module Rails
        # Checks that tests use `have_http_status` instead of equality matchers.
        #
        # @example
        #   # bad
        #   expect(response.status).to be(200)
        #   expect(response.code).to eq("200")
        #
        #   # good
        #   expect(response).to have_http_status(200)
        #
        class HaveHttpStatus < ::RuboCop::Cop::Base
          extend AutoCorrector

          MSG =
            'Prefer `expect(response).%<to>s have_http_status(%<status>s)` ' \
            'over `%<bad_code>s`.'

          RUNNERS = %i[to to_not not_to].to_set
          RESTRICT_ON_SEND = RUNNERS

          # @!method match_status(node)
          def_node_matcher :match_status, <<-PATTERN
            (send
              (send nil? :expect
                $(send (send nil? :response) {:status :code})
              )
              $RUNNERS
              $(send nil? {:be :eq :eql :equal} ({int str} $_))
            )
          PATTERN

          def on_send(node)
            match_status(node) do |response_status, to, match, status|
              return unless status.to_s.match?(/\A\d+\z/)

              message = format(MSG, to: to, status: status,
                                    bad_code: node.source)
              add_offense(node, message: message) do |corrector|
                corrector.replace(response_status, 'response')
                corrector.replace(match.loc.selector, 'have_http_status')
                corrector.replace(match.first_argument, status.to_s)
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/rails/have_http_status.rb
rubocop-rspec-2.25.0 lib/rubocop/cop/rspec/rails/have_http_status.rb
rubocop-rspec-2.24.1 lib/rubocop/cop/rspec/rails/have_http_status.rb
rubocop-rspec-2.24.0 lib/rubocop/cop/rspec/rails/have_http_status.rb
rubocop-rspec-2.23.2 lib/rubocop/cop/rspec/rails/have_http_status.rb
rubocop-rspec-2.23.1 lib/rubocop/cop/rspec/rails/have_http_status.rb
rubocop-rspec-2.23.0 lib/rubocop/cop/rspec/rails/have_http_status.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/rails/have_http_status.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/rails/have_http_status.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/rails/have_http_status.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/rails/have_http_status.rb
rubocop-rspec-2.22.0 lib/rubocop/cop/rspec/rails/have_http_status.rb
rubocop-rspec-2.21.0 lib/rubocop/cop/rspec/rails/have_http_status.rb