Skip to content

Instantly share code, notes, and snippets.

@mmacedo
Created February 23, 2013 07:36
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 mmacedo/5018850 to your computer and use it in GitHub Desktop.
Save mmacedo/5018850 to your computer and use it in GitHub Desktop.
Stub all instances
# 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)
@mmacedo
Copy link
Author

mmacedo commented Feb 23, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment