class RuboCop::Cop::RSpec::StubbedMock

Checks that message expectations do not have a configured response.

@example

# bad
expect(foo).to receive(:bar).with(42).and_return("hello world")

# good (without spies)
allow(foo).to receive(:bar).with(42).and_return("hello world")
expect(foo).to receive(:bar).with(42)

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/rspec/stubbed_mock.rb, line 138
def on_send(node)
  expectation(node) do |expectation, method_name, matcher|
    on_expectation(expectation, method_name, matcher)
  end
end

Private Instance Methods

msg(method_name) click to toggle source
# File lib/rubocop/cop/rspec/stubbed_mock.rb, line 157
def msg(method_name)
  format(MSG,
         method_name: method_name,
         replacement: replacement(method_name))
end
on_expectation(expectation, method_name, matcher) click to toggle source
# File lib/rubocop/cop/rspec/stubbed_mock.rb, line 146
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
replacement(method_name) click to toggle source
# File lib/rubocop/cop/rspec/stubbed_mock.rb, line 163
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