Class: RuboCop::Cop::RSpec::AnyInstance

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/any_instance.rb

Overview

Check that instances are not being stubbed globally.

Prefer instance doubles over stubbing any instance of a class

Examples:

# bad
describe MyClass do
  before { allow_any_instance_of(MyClass).to receive(:foo) }
end

# good
describe MyClass do
  let(:my_instance) { instance_double(MyClass) }

  before do
    allow(MyClass).to receive(:new).and_return(my_instance)
    allow(my_instance).to receive(:foo)
  end
end

Constant Summary collapse

MSG =
'Avoid stubbing using `%<method>s`.'

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

RSpec::Language::ALL, RSpec::Language::RSPEC

Instance Method Summary collapse

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#on_send(node) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/rubocop/cop/rspec/any_instance.rb', line 32

def on_send(node)
  disallowed_stub(node) do |method|
    add_offense(
      node,
      message: format(MSG, method: method)
    )
  end
end