Sha256: 657aee55679353e90c77f8c9fa340a85b850a51b54c4c5770f673e4427d75ae2

Contents?: true

Size: 967 Bytes

Versions: 3

Compression:

Stored size: 967 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpecRails
      # Checks that tests use RSpec `before` hook over Rails `setup` method.
      #
      # @example
      #   # bad
      #   setup do
      #     allow(foo).to receive(:bar)
      #   end
      #
      #   # good
      #   before do
      #     allow(foo).to receive(:bar)
      #   end
      #
      class AvoidSetupHook < ::RuboCop::Cop::RSpec::Base
        extend AutoCorrector

        MSG = 'Use `before` instead of `setup`.'

        # @!method setup_call(node)
        def_node_matcher :setup_call, <<~PATTERN
          (block
            $(send nil? :setup)
            (args) _)
        PATTERN

        def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
          setup_call(node) do |setup|
            add_offense(node) do |corrector|
              corrector.replace setup, 'before'
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-rspec_rails-2.28.2 lib/rubocop/cop/rspec_rails/avoid_setup_hook.rb
rubocop-rspec_rails-2.28.1 lib/rubocop/cop/rspec_rails/avoid_setup_hook.rb
rubocop-rspec_rails-2.28.0 lib/rubocop/cop/rspec_rails/avoid_setup_hook.rb