Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Created July 9, 2015 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myronmarston/b6b71197a192cec158f3 to your computer and use it in GitHub Desktop.
Save myronmarston/b6b71197a192cec158f3 to your computer and use it in GitHub Desktop.
How to get the name of a double.
# 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