Skip to content

Instantly share code, notes, and snippets.

Created April 25, 2012 13:11
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Additions to rr stubbing
# Dynamic method stubbing
# user = Factory(:user)
# callbacks =
# stub_methods("user", callbacks)
def stub_methods(name, methods)
methods.each do |method|
eval "stub(#{name}).#{method}"
# Stub constant and return back its old value without warnings
# stub_const(User, :CONST, value) do
# some code...
# end
def stub_const(klass, name, value)
old_verbose, $VERBOSE = $VERBOSE, nil
old_value = klass.const_get(name)
klass.const_set(name, value)
klass.const_set(name, old_value)
$VERBOSE = old_verbose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment