Sha256: 1d6416345bee266d07c3a463767ba0fe76008643afa0e3995f7c5410e9d1e94d

Contents?: true

Size: 1.53 KB

Versions: 13

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks if `.to`, `not_to` or `to_not` are used.
      #
      # The RSpec::Expectations::ExpectationTarget must use `to`, `not_to` or
      # `to_not` to run. Therefore, this cop checks if other methods are used.
      #
      # @example
      #   # bad
      #   expect(something).kind_of? Foo
      #   is_expected == 42
      #   expect{something}.eq? BarError
      #
      #   # good
      #   expect(something).to be_a Foo
      #   is_expected.to eq 42
      #   expect{something}.to raise_error BarError
      #
      class MissingExpectationTargetMethod < Base
        MSG = 'Use `.to`, `.not_to` or `.to_not` to set an expectation.'
        RESTRICT_ON_SEND = %i[expect is_expected].freeze

        # @!method expect?(node)
        def_node_matcher :expect?, <<~PATTERN
          {
            (send nil? :expect ...)
            (send nil? :is_expected)
          }
        PATTERN

        # @!method expect_block?(node)
        def_node_matcher :expect_block?, <<~PATTERN
          (block #expect? (args) _body)
        PATTERN

        # @!method expectation_without_runner?(node)
        def_node_matcher :expectation_without_runner?, <<~PATTERN
          (send {#expect? #expect_block?} !#Runners.all ...)
        PATTERN

        def on_send(node)
          node = node.parent if node.parent&.block_type?

          expectation_without_runner?(node.parent) do
            add_offense(node.parent.loc.selector)
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 4 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.3/lib/rubocop/cop/rspec/missing_expectation_target_method.rb
rubocop-rspec-3.3.0 lib/rubocop/cop/rspec/missing_expectation_target_method.rb
rubocop-rspec-3.2.0 lib/rubocop/cop/rspec/missing_expectation_target_method.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/missing_expectation_target_method.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/missing_expectation_target_method.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/missing_expectation_target_method.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/missing_expectation_target_method.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/missing_expectation_target_method.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/missing_expectation_target_method.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/missing_expectation_target_method.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/missing_expectation_target_method.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/missing_expectation_target_method.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/missing_expectation_target_method.rb