Sha256: ec9a3ba1e0a0bdb33f3eae513402b231ec367b2479420b54632ac0c80b9840d7

Contents?: true

Size: 1.34 KB

Versions: 20

Compression:

Stored size: 1.34 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 < Cop
        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.'.freeze

        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,
              location: :expression,
              message: format(MSG, hook: hook.source)
            )
          end
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
rubocop-rspec-1.32.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.31.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.30.1 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.30.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.29.1 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.29.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.28.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.27.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.26.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.25.1 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.25.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.24.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.23.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.22.2 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.22.1 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.22.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.21.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.20.1 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.20.0 lib/rubocop/cop/rspec/before_after_all.rb
rubocop-rspec-1.19.0 lib/rubocop/cop/rspec/before_after_all.rb