Skip to content

Instantly share code, notes, and snippets.

@edwardloveall
Created January 4, 2018 15:09
Show Gist options
  • Save edwardloveall/85721ba277326424cb859d0249448bd1 to your computer and use it in GitHub Desktop.
Save edwardloveall/85721ba277326424cb859d0249448bd1 to your computer and use it in GitHub Desktop.
Quick test of RSpec's Instance double
RSpec.configure do |config|
config.mock_with :rspec do |mocks|
mocks.verify_doubled_constant_names = true
end
end
class Greeting
def greet
'Hi'
end
end
RSpec.describe Greeting do
it 'can mock existing methods' do
greeting = instance_double(Greeting)
allow(greeting).to receive(:greet).and_return('Bye')
expect(greeting.greet).to eq('Bye')
end
it 'can mock existing methods' do
greeting = instance_double(Greeting)
allow(greeting).to receive(:foo).and_return('Bye')
expect(greeting.foo).to eq('Bye')
end
end
@edwardloveall
Copy link
Author

edwardloveall commented Jan 4, 2018

This page has more info: https://relishapp.com/rspec/rspec-mocks/docs/verifying-doubles/using-an-instance-double

What I'm showing here is that you can mock existing methods only. So the Greeting class has a greet method, but not a foo method, so the first test passes, but the second doesn't.

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