Skip to content

Files

Latest commit

 

History

History
33 lines (23 loc) · 811 Bytes

RSpec-AnyInstance.md

File metadata and controls

33 lines (23 loc) · 811 Bytes

Pattern: Use of globally stubbed instance

Issue: -

Description

Checks 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

Further Reading