class RuboCop::Cop::RSpec::MultipleSubjects
a ‘before` hook on their own
This is enough of an edge case that people can just move this to
- If subjects are defined with `subject!` then we don’t autocorrect.
be dead code and we remove the overwritten subject definitions.
- If multiple unnamed subjects are defined though then this can only
case they are replaced with ‘let`.
definition) are effectively being used to define helpers. In this
that the overwritten subjects (all subjects except the last
- If multiple named subjects are defined then this probably indicates
duplication:
The autocorrect behavior for this cop depends on the type of
end
subject(:post) { Post.new }
let(:user) { User.new }
describe Foo do
# good
end
subject(:post) { Post.new }
subject(:user) { User.new }
describe Foo do
# bad
@example
Checks if an example group defines `subject` multiple times.
def autocorrect(node)
def autocorrect(node) return unless node.method_name.equal?(:subject) # Ignore `subject!` if named_subject?(node) rename_autocorrect(node) else remove_autocorrect(node) end end
def named_subject?(node)
def named_subject?(node) node.send_node.arguments? end
def on_block(node)
def on_block(node) return unless example_group?(node) subjects = RuboCop::RSpec::ExampleGroup.new(node).subjects subjects[0...-1].each do |subject| add_offense(subject, location: :expression) end end
def remove_autocorrect(node)
def remove_autocorrect(node) lambda do |corrector| corrector.remove(node.loc.expression) end end
def rename_autocorrect(node)
def rename_autocorrect(node) lambda do |corrector| corrector.replace(node.send_node.loc.selector, 'let') end end