Last active
October 24, 2019 16:24
-
-
Save SViccari/ba8bcccc489ff1e10199b37f7e378901 to your computer and use it in GitHub Desktop.
RSpec Double vs 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 Double vs Instance Double | |
# stub FeatureToggle using a double | |
def stub_feature_toggle_using_double | |
double(FeatureToggle, on?: true).tap do |feature_toggle| | |
allow(FeatureToggle).to receive(:new).and_return(feature_toggle) | |
end | |
end | |
# In the above example, we're stubbing out the response for "on?". | |
# In the event we typo "on?" and write "onn?", running the test returns the error message: | |
# => <Double FeatureToggle> received unexpected message :on? | |
# stub FeatureToggle using instance double | |
def stub_feature_toggle_using_double | |
instance_double(FeatureToggle, on?: true).tap do |feature_toggle| | |
allow(FeatureToggle).to receive(:new).and_return(feature_toggle) | |
end | |
end | |
# using the same example, in the event we typo "on?" and write "onn?", running the test returns the error message: | |
# => the FeatureToggle class does not implement the instance method: onn? | |
# instance doubles will verify that the method(s) being stubbed exist on the underlying object | |
# and provide a clearer error message when a non-existent method is being stubbed. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment