class RuboCop::Cop::RSpec::StubbedMock
expect(foo).to receive(:bar).with(42)
allow(foo).to receive(:bar).with(42).and_return(“hello world”)
# good (without spies)
expect(foo).to receive(:bar).with(42).and_return(“hello world”)
# bad
@example
Checks that message expectations do not have a configured response.
def msg(method_name)
def msg(method_name) format(MSG, method_name: method_name, replacement: replacement(method_name)) end
def on_expectation(expectation, method_name, matcher)
def on_expectation(expectation, method_name, matcher) flag_expectation = lambda do add_offense(expectation, message: msg(method_name)) end matcher_with_configured_response(matcher, &flag_expectation) matcher_with_return_block(matcher, &flag_expectation) matcher_with_hash(matcher, &flag_expectation) matcher_with_blockpass(matcher, &flag_expectation) end
def on_send(node)
def on_send(node) expectation(node, &method(:on_expectation)) end
def replacement(method_name)
def replacement(method_name) case method_name when :expect :allow when :is_expected 'allow(subject)' when :expect_any_instance_of :allow_any_instance_of end end