Class: RuboCop::Cop::Vicenzo::RSpec::DerivedPremises

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

Overview

Do not derive a premise from another premise.

let(:params) { base_params.merge(age: 10) } does not mutate anything, but it splits a single premise across two definitions: to know what params holds in a context you have to read another let — usually one that exists only to be modified. Declare the complete value in the context that needs it.

Only premise definitions (let, let_it_be, subject) are inspected, and only when the copy starts from another premise: deriving from a factory or from a literal is untouched.

Examples:

# bad

let(:base_params) { { name: 'Ada' } }
let(:params) { base_params.merge(age: 10) }

# good

context 'when the age is informed' do
  let(:params) { { name: 'Ada', age: 10 } }
end

context 'when the age is unknown' do
  let(:params) { { name: 'Ada' } }
end
# good — the source is a factory, not another premise

let(:params) { attributes_for(:order).merge(value: 10) }

Constant Summary collapse

MSG =
'Do not derive the premise `%<name>s` from `%<source>s`. ' \
'Declare the complete value in the context that needs it.'
DERIVING_METHODS =
%i[merge deep_merge reverse_merge dup clone except slice].freeze

Constants included from PremiseTracking

PremiseTracking::SETUP_HOOKS

Instance Method Summary collapse

Methods included from PremiseTracking

#premise_name, #root_receiver_name

Instance Method Details

#on_block(node) ⇒ Object Also known as: on_numblock



46
47
48
49
50
# File 'lib/rubocop/cop/vicenzo/rspec/derived_premises.rb', line 46

def on_block(node)
  return unless example_group?(node) && outermost_example_group?(node)

  walk_example_group(node, all_premises(node))
end