Class: RuboCop::Cop::RSpec::IndexedLet

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/indexed_let.rb

Overview

Do not set up test data using indexes (e.g., ‘item_1`, `item_2`).

It makes reading the test harder because it’s not clear what exactly is tested by this particular example.

Examples:

‘Max: 1 (default)`

# bad
let(:item_1) { create(:item) }
let(:item_2) { create(:item) }

let(:item1) { create(:item) }
let(:item2) { create(:item) }

# good

let(:visible_item) { create(:item, visible: true) }
let(:invisible_item) { create(:item, visible: false) }

‘Max: 2`

# bad
let(:item_1) { create(:item) }
let(:item_2) { create(:item) }
let(:item_3) { create(:item) }

# good
let(:item_1) { create(:item) }
let(:item_2) { create(:item) }

Constant Summary collapse

MSG =
'This `let` statement uses index in its name. Please give it ' \
'a meaningful name.'

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_or_numblock_pattern, #block_pattern, #numblock_pattern, #send_pattern

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#let_name(node) ⇒ Object



39
40
41
42
43
44
# File 'lib/rubocop/cop/rspec/indexed_let.rb', line 39

def_node_matcher :let_name, <<~PATTERN
  {
    (block (send nil? #Helpers.all ({str sym} $_) ...) ...)
    (send nil? #Helpers.all ({str sym} $_) block_pass)
  }
PATTERN

#on_block(node) ⇒ Object

rubocop:disable InternalAffairs/NumblockHandler



46
47
48
49
50
51
52
53
54
55
# File 'lib/rubocop/cop/rspec/indexed_let.rb', line 46

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
  return unless spec_group?(node)

  children = node.body&.child_nodes
  return unless children

  filter_indexed_lets(children).each do |let_node|
    add_offense(let_node)
  end
end