Sha256: 5c1f2dc17c6f9c9d21994673ede30b6378facce9d92d9af0b9f4c9f55a96575b

Contents?: true

Size: 1.04 KB

Versions: 53

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Prefer `not_to receive(...)` over `receive(...).never`.
      #
      # @example
      #   # bad
      #   expect(foo).to receive(:bar).never
      #
      #   # good
      #   expect(foo).not_to receive(:bar)
      #
      class ReceiveNever < Base
        extend AutoCorrector
        MSG = 'Use `not_to receive` instead of `never`.'
        RESTRICT_ON_SEND = %i[never].freeze

        # @!method method_on_stub?(node)
        def_node_search :method_on_stub?, '(send nil? :receive ...)'

        def on_send(node)
          return unless node.method?(:never) && method_on_stub?(node)

          add_offense(node.loc.selector) do |corrector|
            autocorrect(corrector, node)
          end
        end

        private

        def autocorrect(corrector, node)
          corrector.replace(node.parent.loc.selector, 'not_to')
          range = node.loc.dot.with(end_pos: node.loc.selector.end_pos)
          corrector.remove(range)
        end
      end
    end
  end
end

Version data entries

53 entries across 51 versions & 7 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/receive_never.rb
rubocop-rspec-3.3.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-3.2.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/receive_never.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/receive_never.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.31.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.30.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.29.2 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.29.1 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.29.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.28.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.27.1 lib/rubocop/cop/rspec/receive_never.rb