Skip to content

Instantly share code, notes, and snippets.

@route
Created April 25, 2012 13:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save route/2489600 to your computer and use it in GitHub Desktop.
Save route/2489600 to your computer and use it in GitHub Desktop.
Additions to rr stubbing
# Dynamic method stubbing
# user = Factory(:user)
# callbacks = User._create_callbacks.map(&:filter)
# stub_methods("user", callbacks)
def stub_methods(name, methods)
methods.each do |method|
eval "stub(#{name}).#{method}"
end
end
# 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)
yield
ensure
klass.const_set(name, old_value)
$VERBOSE = old_verbose
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment