Sha256: d4f1ad70e855063278fc4763bff7997bdd412e54037762c6176e4068adf77d99

Contents?: true

Size: 1.3 KB

Versions: 7

Compression:

Stored size: 1.3 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.'

        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

7 entries across 7 versions & 1 rubygems

Version Path
rubocop-rspec-2.0.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-2.0.0.pre lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.44.1 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.44.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.43.2 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.43.1 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.43.0 lib/rubocop/cop/rspec/before_after_all.rb