Sha256: 2ab54c08c9b7f1a6556873ca4582f9964064884ccd3f70195492c271cb037e91

Contents?: true

Size: 1.4 KB

Versions: 29

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Check that before/after(:all) isn't being used.
      #
      # @example
      #   # bad
      #   #
      #   # Faster but risk of state leaking between examples
      #   #
      #   describe MyClass do
      #     before(:all) { Widget.create }
      #     after(:all) { 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 = %i[before after].freeze

        # @!method before_or_after_all(node)
        def_node_matcher :before_or_after_all, <<-PATTERN
          $(send _ {:before :after} (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

29 entries across 27 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.13.1/lib/rubocop/cop/rspec/before_after_all.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-2.25.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-2.24.1 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-2.24.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-2.23.2 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-2.23.1 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-2.23.0 lib/rubocop/cop/rspec/before_after_all.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/before_after_all.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/before_after_all.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.13.1/lib/rubocop/cop/rspec/before_after_all.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/before_after_all.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-2.22.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-2.21.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-2.20.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-2.19.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-2.18.1 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-2.18.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-2.17.1 lib/rubocop/cop/rspec/before_after_all.rb