class RuboCop::Cop::RSpec::SubjectStub
end
end
allow(bar).to receive(:qux?).and_return(true)
before do
subject(:bar) { baz }
describe Foo do
# bad
@example
@see robots.thoughtbot.com/don-t-stub-the-system-under-test<br><br>Checks for stubbed test subjects.
def expectation?(node)
def expectation?(node) return if all_matcher?(node) receive_message?(node) end
def find_subject(node, parent: nil, &block)
- Yieldparam: parent - parent of subject definition
Yieldparam: subject_name - name of subject being defined
Parameters:
-
parent
(RuboCop::Node, nil
) -- -
node
(RuboCop::Node
) --
def find_subject(node, parent: nil, &block) subject(node) { |name| yield(name, parent) } node.each_child_node do |child| find_subject(child, parent: node, &block) end end
def find_subject_expectation(node, subject_name, &block)
- Yield: - message expectation
Parameters:
-
subject_name
(Symbol
) -- name of subject -
node
(RuboCop::Node
) --
def find_subject_expectation(node, subject_name, &block) # Do not search node if it is an example group with its own subject. return if example_group?(node) && redefines_subject?(node) # Yield the current node if it is a message expectation. yield(node) if message_expectation?(node, subject_name) # Recurse through node's children looking for a message expectation. node.each_child_node do |child| find_subject_expectation(child, subject_name, &block) end end
def find_subject_stub(node, &block)
- Yield: - message expectations for subject
Parameters:
-
node
(RuboCop::Node
) -- example group
def find_subject_stub(node, &block) find_subject(node) do |subject_name, context| find_subject_expectation(context, subject_name, &block) end end
def on_block(node)
def on_block(node) return unless example_group?(node) find_subject_stub(node) do |stub| add_offense(stub, location: :expression) end end
def redefines_subject?(node)
-
(Boolean)
-
Parameters:
-
node
(RuboCop::Node
) --
def redefines_subject?(node) node.each_child_node.any? do |child| subject(child) || redefines_subject?(child) end end