Skip to content

Instantly share code, notes, and snippets.

@djuber
Created December 5, 2022 16:27
Show Gist options
  • Save djuber/cf0fec3b996b316ced18908fcbdf6e5b to your computer and use it in GitHub Desktop.
Save djuber/cf0fec3b996b316ced18908fcbdf6e5b to your computer and use it in GitHub Desktop.
leaked double reproduction
class SomeClass
def self.client
true
end
end
class UsesClient
def self.call
client.nil?
end
def self.client
@client ||= SomeClass.client
end
end
RSpec.describe 'tangled web we weave' do
let(:client) { double(:client) }
before do
allow(SomeClass).to receive(:client).and_return(client)
end
it 'does one thing' do
expect(client).to receive(:nil?)
UsesClient.call
end
it 'does another thing' do
expect(client).to receive(:nil?)
UsesClient.call
end
end
@djuber
Copy link
Author

djuber commented Dec 5, 2022

the memoized UsesClient.client value gets captured in UsesClient (the class object) and the double supplied to the first example pollutes the environment of the second example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment