forked from rubocop/rubocop-minitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assertion_in_lifecycle_hook.rb
43 lines (39 loc) · 1.08 KB
/
assertion_in_lifecycle_hook.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# frozen_string_literal: true
module RuboCop
module Cop
module Minitest
# Checks for usage of assertions in lifecycle hooks.
#
# @example
# # bad
# class FooTest < Minitest::Test
# def setup
# assert_equal(foo, bar)
# end
# end
#
# # good
# class FooTest < Minitest::Test
# def test_something
# assert_equal(foo, bar)
# end
# end
#
class AssertionInLifecycleHook < Base
include MinitestExplorationHelpers
MSG = 'Do not use `%<assertion>s` in `%<hook>s` hook.'
def on_class(class_node)
return unless test_class?(class_node)
lifecycle_hooks(class_node).each do |hook_node|
hook_node.each_descendant(:send) do |node|
if assertion_method?(node)
message = format(MSG, assertion: node.method_name, hook: hook_node.method_name)
add_offense(node, message: message)
end
end
end
end
end
end
end
end