Class: RuboCop::Cop::Vicenzo::RSpec::IterationInsideExample
- Inherits:
-
RSpec::Base
- Object
- RSpec::Base
- RuboCop::Cop::Vicenzo::RSpec::IterationInsideExample
- Defined in:
- lib/rubocop/cop/vicenzo/rspec/iteration_inside_example.rb
Overview
Do not use expect inside an iteration within an example.
Placing expect calls inside an iteration block (e.g. each) makes
tests implicit and hard to debug: when the assertion fails it is unclear
which element caused the failure, and not all elements may represent the
same condition. Using iteration to build or transform data before calling
expect is fine; the problem is calling expect inside the iteration.
Write explicit assertions for each relevant case instead.
Constant Summary collapse
- MSG =
'Do not call `expect` inside an iteration. ' \ 'Write explicit assertions instead.'
- ENUMERATION_METHODS =
%i[each each_with_index each_with_object].freeze
Instance Method Summary collapse
- #enumeration_block?(node) ⇒ Object
- #on_block(node) ⇒ Object (also: #on_numblock)
Instance Method Details
#enumeration_block?(node) ⇒ Object
48 49 50 51 52 |
# File 'lib/rubocop/cop/vicenzo/rspec/iteration_inside_example.rb', line 48 def_node_matcher :enumeration_block?, <<~PATTERN (block (send _ {#{ENUMERATION_METHODS.map(&:inspect).join(' ')}} ...) ...) PATTERN |
#on_block(node) ⇒ Object Also known as: on_numblock
54 55 56 57 58 |
# File 'lib/rubocop/cop/vicenzo/rspec/iteration_inside_example.rb', line 54 def on_block(node) return unless example?(node) find_iterations_with_assertions(node.body) end |