Class: RuboCop::Cop::Minitest::MultipleAssertions

Inherits:
Base
  • Object
show all
Includes:
RuboCop::Cop::MinitestExplorationHelpers
Defined in:
lib/rubocop/cop/minitest/multiple_assertions.rb

Overview

Checks if test cases contain too many assertion calls. If conditional code with assertions is used, the branch with maximum assertions is counted. The maximum allowed assertion calls is configurable.

Examples:

Max: 1

# bad
class FooTest < Minitest::Test
  def test_asserts_twice
    assert_equal(42, do_something)
    assert_empty(array)
  end
end

# good
class FooTest < Minitest::Test
  def test_asserts_once
    assert_equal(42, do_something)
  end

  def test_another_asserts_once
    assert_empty(array)
  end
end

Constant Summary collapse

MSG =
'Test case has too many assertions [%<total>d/%<max>d].'

Constants included from RuboCop::Cop::MinitestExplorationHelpers

RuboCop::Cop::MinitestExplorationHelpers::ASSERTION_PREFIXES, RuboCop::Cop::MinitestExplorationHelpers::LIFECYCLE_HOOK_METHODS, RuboCop::Cop::MinitestExplorationHelpers::LIFECYCLE_HOOK_METHODS_IN_ORDER

Instance Method Summary collapse

Instance Method Details

#on_class(class_node) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubocop/cop/minitest/multiple_assertions.rb', line 37

def on_class(class_node)
  return unless test_class?(class_node)

  test_cases(class_node).each do |node|
    assertions_count = assertions_count(node.body)

    next unless assertions_count > max_assertions

    self.max = assertions_count

    message = format(MSG, total: assertions_count, max: max_assertions)
    add_offense(node, message: message)
  end
end