Class: RuboCop::Cop::Vicenzo::RSpec::NestedLetRedefinition

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

Overview

Do not define the same let in nested example groups.

It makes the tests more dificult to read and indicates that exists hidden scenarios (contexts)

Examples:

Hidden context


# bad

describe '#authorized?' do
  subject(:action) { create(:action) }
  let(:user) { create(:user, :admin) }

  it { expect(action).to be_authorized(user) }

  context 'when user is not admin' do
    let(:user) { create(:user, :analyst) }

    it { expect(action).not_to be_authorized(user) }
  end
end

# good

describe '#authorized?' do
  subject(:action) { create(:action) }

  context 'when user is not admin' do
    let(:user) { create(:user, :analyst) }

    it { expect(action).not_to be_authorized(user) }
  end

  context 'when user is admin' do # this context was hidden
    let(:user) { create(:user, :admin) }

    it { expect(action).to be_authorized(user) }
  end
end

Constant Summary collapse

MSG =
'Let `:%<name>s` is already defined in ancestor(s) block(s) at: %<definitions>s.'

Instance Method Summary collapse

Instance Method Details

#let_it_be?(node) ⇒ Object



65
66
67
68
69
70
# File 'lib/rubocop/cop/vicenzo/rspec/nested_let_redefinition.rb', line 65

def_node_matcher :let_it_be?, <<~PATTERN
  {
    (block (send nil? {:let_it_be :let_it_be!} ...) ...)
    (send nil? {:let_it_be :let_it_be!} _ block_pass)
  }
PATTERN

#let_it_be_name(node) ⇒ Object



57
58
59
60
61
62
# File 'lib/rubocop/cop/vicenzo/rspec/nested_let_redefinition.rb', line 57

def_node_matcher :let_it_be_name, <<~PATTERN
  {
    (block (send nil? {:let_it_be :let_it_be!} ({str sym} $_) ...) ...)
    (send nil? {:let_it_be :let_it_be!} ({str sym} $_) block_pass)
  }
PATTERN

#let_name(node) ⇒ Object



49
50
51
52
53
54
# File 'lib/rubocop/cop/vicenzo/rspec/nested_let_redefinition.rb', line 49

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 Also known as: on_numblock



72
73
74
# File 'lib/rubocop/cop/vicenzo/rspec/nested_let_redefinition.rb', line 72

def on_block(node)
  check_let_redefinitions(node, {}) if example_group?(node)
end