lib/rubocop/cop/rspec/before_after_all.rb in rubocop-rspec-1.12.0 vs lib/rubocop/cop/rspec/before_after_all.rb in rubocop-rspec-1.13.0
- old
+ new
@@ -22,28 +22,22 @@
# 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
+ 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
- BEFORE_AFTER_METHODS = [
- :before,
- :after
- ].freeze
+ def_node_matcher :before_or_after_all, <<-PATTERN
+ $(send _ {:before :after} (sym {:all :context}))
+ PATTERN
- 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)
+ before_or_after_all(node) do |hook|
+ add_offense(node, :expression, format(MSG, hook: hook.source))
+ end
end
end
end
end
end