Created
January 4, 2018 15:09
-
-
Save edwardloveall/85721ba277326424cb859d0249448bd1 to your computer and use it in GitHub Desktop.
Quick test of RSpec's Instance double
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Greetingclass has agreetmethod, but not afoomethod, so the first test passes, but the second doesn't.