Sha256: 896dc877d8c0db4f51d286dec4f9f4f697ad3e05ba2af52e9631ba25a5a12442

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 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
        MESSAGE = 'Beware of using `before/after(:all)` 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 '\
          '`before(:all)` are not rolled back.'.freeze

        BEFORE_AFTER_METHODS = [
          :before,
          :after
        ].freeze

        ALL_PAIR = s(:sym, :all)
        CONTEXT_PAIR = s(:sym, :context)

        def on_send(node)
          _receiver, method_name, *args = *node
          return unless BEFORE_AFTER_METHODS.include?(method_name)
          return unless args.include?(ALL_PAIR) || args.include?(CONTEXT_PAIR)

          add_offense(node, :expression, MESSAGE)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-rspec-1.12.0 lib/rubocop/cop/rspec/before_after_all.rb