Sha256: 18c54b6f9ab4a3644e576ed098cce85ad2a071bce8b3cf0ea17953d7a3c85fae

Contents?: true

Size: 960 Bytes

Versions: 5

Compression:

Stored size: 960 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::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

5 entries across 5 versions & 2 rubygems

Version Path
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec_rails-2.30.0/lib/rubocop/cop/rspec_rails/avoid_setup_hook.rb
rubocop-rspec_rails-2.30.0 lib/rubocop/cop/rspec_rails/avoid_setup_hook.rb
rubocop-rspec_rails-2.29.1 lib/rubocop/cop/rspec_rails/avoid_setup_hook.rb
rubocop-rspec_rails-2.29.0 lib/rubocop/cop/rspec_rails/avoid_setup_hook.rb
rubocop-rspec_rails-2.28.3 lib/rubocop/cop/rspec_rails/avoid_setup_hook.rb