Sha256: 28a5a4730ecf7d05b08706a6573ce78cc357333616c99b11958f7ec194ef8d00

Contents?: true

Size: 1.35 KB

Versions: 24

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Check that before/after(:all/:context) isn't being used.
      #
      # @example
      #   # bad - Faster but risk of state leaking between examples
      #   describe MyClass do
      #     before(:all) { Widget.create }
      #     after(:context) { Widget.delete_all }
      #   end
      #
      #   # good - Slower but examples are properly isolated
      #   describe MyClass do
      #     before(:each) { Widget.create }
      #     after(:each) { Widget.delete_all }
      #   end
      #
      class BeforeAfterAll < Base
        MSG = 'Beware of using `%<hook>s` as it may cause state to leak ' \
              'between tests. If you are using `rspec-rails`, and ' \
              '`use_transactional_fixtures` is enabled, then records created ' \
              'in `%<hook>s` are not automatically rolled back.'

        RESTRICT_ON_SEND = Set[:before, :after].freeze

        # @!method before_or_after_all(node)
        def_node_matcher :before_or_after_all, <<~PATTERN
          $(send _ RESTRICT_ON_SEND (sym {:all :context}))
        PATTERN

        def on_send(node)
          before_or_after_all(node) do |hook|
            add_offense(
              node,
              message: format(MSG, hook: hook.source)
            )
          end
        end
      end
    end
  end
end

Version data entries

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