Created
February 23, 2013 07:36
-
-
Save mmacedo/5018850 to your computer and use it in GitHub Desktop.
Stub all instances
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
# Create a class method (anyone, anywhere) | |
User.stub :save | |
# Save original constructor | |
new_method = User.method(:new) | |
# Stub constructor | |
User.stub(:new).and_return do |*args| | |
# Call original constructor | |
instance = new_method.call(*args) | |
# Save original save method | |
save_method = instance.method(:save) | |
# Stub save method | |
instance.stub(:save).and_return do |*args| | |
# Call newly created class method | |
User.save(*args) | |
# Call original save method and return it | |
save_method.call(*args) | |
end | |
# Return new instance | |
instance | |
end | |
# User.any_instance.should_receive(:save).at_least(:once) | |
User.should_receive(:save).at_least(:once) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by https://gist.github.com/chischaschos/853602
To fix http://stackoverflow.com/q/9800992