Class: RuboCop::Cop::RSpec::BeforeAfterAll

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/before_after_all.rb

Overview

Check that before/after(:all) isn't being used.

Examples:

# 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

Constant Summary collapse

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

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #send_pattern

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#before_or_after_all(node) ⇒ Object


35
36
37
# File 'lib/rubocop/cop/rspec/before_after_all.rb', line 35

def_node_matcher :before_or_after_all, <<-PATTERN
  $(send _ {:before :after} (sym {:all :context}))
PATTERN

#on_send(node) ⇒ Object


39
40
41
42
43
44
45
46
# File 'lib/rubocop/cop/rspec/before_after_all.rb', line 39

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