Sha256: 653517927d8112252ab0fcadf2a50fa208e0427b6214170cc5023cb4d6d3baa6

Contents?: true

Size: 1.37 KB

Versions: 13

Compression:

Stored size: 1.37 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)
        #
        #   # good
        #   expect(response).to have_http_status(200)
        #
        class HaveHttpStatus < Base
          extend AutoCorrector

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

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

          def on_send(node)
            match_status(node) do |response_status, to, match, status|
              message = format(MSG, to: to, match: match.source, status: status)
              add_offense(node, message: message) do |corrector|
                corrector.replace(response_status.source_range, 'response')
                corrector.replace(match.loc.selector, 'have_http_status')
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 11 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.12.1/lib/rubocop/cop/rspec/rails/have_http_status.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.13.1/lib/rubocop/cop/rspec/rails/have_http_status.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.12.1/lib/rubocop/cop/rspec/rails/have_http_status.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.13.1/lib/rubocop/cop/rspec/rails/have_http_status.rb
rubocop-rspec-2.15.0 lib/rubocop/cop/rspec/rails/have_http_status.rb
rubocop-rspec-2.14.2 lib/rubocop/cop/rspec/rails/have_http_status.rb
rubocop-rspec-2.14.1 lib/rubocop/cop/rspec/rails/have_http_status.rb
rubocop-rspec-2.14.0 lib/rubocop/cop/rspec/rails/have_http_status.rb
rubocop-rspec-2.13.2 lib/rubocop/cop/rspec/rails/have_http_status.rb
rubocop-rspec-2.13.1 lib/rubocop/cop/rspec/rails/have_http_status.rb
rubocop-rspec-2.13.0 lib/rubocop/cop/rspec/rails/have_http_status.rb
rubocop-rspec-2.12.1 lib/rubocop/cop/rspec/rails/have_http_status.rb
rubocop-rspec-2.12.0 lib/rubocop/cop/rspec/rails/have_http_status.rb