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

Inherits:
Cop
  • 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.'.freeze

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION

Constants included from RSpec::Language

RSpec::Language::ALL

Instance Method Summary collapse

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#on_send(node) ⇒ Object



36
37
38
39
40
# File 'lib/rubocop/cop/rspec/before_after_all.rb', line 36

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