Skip to content

Instantly share code, notes, and snippets.

@mcmire
Created February 28, 2009 23:12
Show Gist options
  • Save mcmire/72145 to your computer and use it in GitHub Desktop.
Save mcmire/72145 to your computer and use it in GitHub Desktop.
Various ideas for improving not_a_mock so that we don't have to track methods manually
# Various ideas for improving not_a_mock so that we don't have to track methods manually
# Note that none of these work, so don't try them
class Object
def track_all_methods
ancestors = self.class.ancestors - [Object, Kernel, ...] # insert all of Ruby Core classes here
instance_methods = ancestors.map {|x| x.instance_methods(false) }
class_methods = ancestors.map {|x| x.singleton_methods(false) }
methods = (instance_methods + class_methods).flatten.map {|x| x.to_sym }
track_methods(*methods)
end
end
# This doesn't work if you're tracking a method that's called deeper than the method you're calling
class Spy
def initialize(object)
@object = object
end
def method_missing(name, *args, &block)
@object.track_method(name)
@object.send(name, *args, &block)
end
def track_methods(*methods)
@object.untrack_methods(*methods)
@object.track_methods(*methods)
end
alias_method :track_method, :track_methods
def should(*args)
@object.send(:should, *args)
end
def should_not(*args)
@object.send(:should_not, *args)
end
end
module Kernel
def spy(object)
Spy.new(object)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment