Sha256: c8df93bd636d18e8541fa83c134661092766cef67ba93a02f989b16f18ea70bb

Contents?: true

Size: 1 KB

Versions: 29

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      module Rails
        # 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 < 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
end

Version data entries

29 entries across 27 versions & 4 rubygems

Version Path
rubocop-rspec-2.17.0 lib/rubocop/cop/rspec/rails/avoid_setup_hook.rb
rubocop-rspec-2.16.0 lib/rubocop/cop/rspec/rails/avoid_setup_hook.rb
rubocop-rspec-2.15.0 lib/rubocop/cop/rspec/rails/avoid_setup_hook.rb
rubocop-rspec-2.14.2 lib/rubocop/cop/rspec/rails/avoid_setup_hook.rb
rubocop-rspec-2.14.1 lib/rubocop/cop/rspec/rails/avoid_setup_hook.rb
rubocop-rspec-2.14.0 lib/rubocop/cop/rspec/rails/avoid_setup_hook.rb
rubocop-rspec-2.13.2 lib/rubocop/cop/rspec/rails/avoid_setup_hook.rb
rubocop-rspec-2.13.1 lib/rubocop/cop/rspec/rails/avoid_setup_hook.rb
rubocop-rspec-2.13.0 lib/rubocop/cop/rspec/rails/avoid_setup_hook.rb