class RuboCop::Cop::RSpec::ReturnFromStub
allow(Foo).to receive(:bar) { bar.baz }
# also good as the returned value is dynamic
expect(Foo).to receive(:bar).and_return(“baz”)
allow(Foo).to receive(:bar).and_return(“baz”)
# good
expect(Foo).to receive(:bar) { “baz” }
allow(Foo).to receive(:bar) { “baz” }
# bad
@example ‘EncorcedStyle: and_return`
allow(Foo).to receive(:bar).and_return(bar.baz)
# also good as the returned value is dynamic
expect(Foo).to receive(:bar) { “baz” }
allow(Foo).to receive(:bar) { “baz” }
# good
expect(Foo).to receive(:bar).and_return(“baz”)
allow(Foo).to receive(:bar).and_return(“baz”)
# bad
@example `EncorcedStyle: block`
This cop can be configured using the `EnforcedStyle` option
are the result would be different
where the returned value is constant. Ignores dynamic returned values
Enforces either `and_return` or block-style return in the cases
Checks for consistent style of stub’s return setting.
def dynamic?(node)
def dynamic?(node) if node.array_type? return node.each_child_node.any? { |child| dynamic?(child) } end !node.literal? end
def on_block(node)
def on_block(node) return unless style == :and_return receive_with_block(node) do |args| add_offense(node, :expression, MSG_AND_RETURN) unless dynamic?(args) end end
def on_send(node)
def on_send(node) return unless style == :block and_return_value(node) do |args| add_offense(node, :expression, MSG_BLOCK) unless dynamic?(args) end end