Sha256: e468b4ece2b8bc415fe3e8beb03d11442f1884311c32eae87d9d9e1c705072ed

Contents?: true

Size: 996 Bytes

Versions: 9

Compression:

Stored size: 996 Bytes

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`.'

        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

9 entries across 9 versions & 1 rubygems

Version Path
rubocop-rspec-2.1.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.0.1 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.0.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-2.0.0.pre lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-1.44.1 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-1.44.0 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-1.43.2 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-1.43.1 lib/rubocop/cop/rspec/receive_never.rb
rubocop-rspec-1.43.0 lib/rubocop/cop/rspec/receive_never.rb