Class: RuboCop::Cop::Vicenzo::RSpec::MutatedPremises
- Inherits:
-
RSpec::Base
- Object
- RSpec::Base
- RuboCop::Cop::Vicenzo::RSpec::MutatedPremises
- Includes:
- PremiseTracking
- Defined in:
- lib/rubocop/cop/vicenzo/rspec/mutated_premises.rb
Overview
Do not mutate the premises of an example.
A let, let_it_be or subject is the premise a reader assumes true when reading the file. Mutating it
somewhere else breaks that in two ways:
- The file lies. You read
let(:params) { { name: 'Ada' } }, run it, and the value is different — the explanation is abeforedozens of lines away. - It breeds flakiness.
letis lazy, so whichever example materialises it first decides the final state. A context that touches the premises in another order silently changes the result.
Persisting changes while building a premise is flagged wherever it happens inside the definition, including
inline forms such as tap, because a factory trait or transient is always available instead. Collection
mutations and attribute writers are flagged when they change a premise declared in an ancestor group, so
building a local value inside the definition itself stays allowed.
Examples are not inspected: there the mutation usually is the behaviour under test.
Constant Summary collapse
- MSG_MUTATION =
'Do not mutate the premise `%<name>s`. Declare the final state where it is needed — ' \ 'factory trait, factory transient, or creation order.'
- MSG_PERSISTENCE =
'Do not persist changes while building `%<name>s`. Move it to a factory ' \ 'trait/transient, or declare the final state in the context that needs it.'
- PERSISTENCE_METHODS =
%i[ update update! update_attribute update_column update_columns save save! destroy destroy! touch increment! decrement! toggle! ].freeze
- COLLECTION_METHODS =
%i[<< push concat unshift store merge! deep_merge! clear delete].freeze
Constants included from PremiseTracking
Instance Method Summary collapse
- #on_block(node) ⇒ Object (also: #on_numblock)
Methods included from PremiseTracking
#premise_name, #root_receiver_name
Instance Method Details
#on_block(node) ⇒ Object Also known as: on_numblock
85 86 87 88 89 |
# File 'lib/rubocop/cop/vicenzo/rspec/mutated_premises.rb', line 85 def on_block(node) return unless example_group?(node) && outermost_example_group?(node) walk_example_group(node, all_premises(node)) end |