Created
July 9, 2015 17:48
-
-
Save myronmarston/b6b71197a192cec158f3 to your computer and use it in GitHub Desktop.
How to get the name of a double.
This file contains 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
# to get the name, you can use `instance_variable_get`: | |
tweet = double("Twitter::Tweet") | |
tweet.instance_variable_get(:@name) # => "Twitter::Tweet" | |
# that's clearly violating the public API, though. Another way is to pass it as a stub: | |
tweet = double("Twitter::Tweet", name: "Twitter::Tweet") | |
tweet.name # => "Twitter::Tweet" | |
# If you like that approach, you could create a new `named_double` | |
# helper method: | |
module NamedDoubles | |
def named_double(name, stubs={}) | |
double(name, stubs.merge(name: name)) | |
end | |
end | |
RSpec.configure do |config| | |
config.include NameableDoubles | |
end | |
tweet = named_double("Twitter::Tweet") | |
tweet.name # => "Twitter::Tweet" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment