class ActiveSupport::Testing::SimpleStubs
:nodoc:
def initialize
def initialize @stubs = {} end
def stub_object(object, method_name, &block)
def stub_object(object, method_name, &block) key = [object.object_id, method_name] if stub = @stubs[key] unstub_object(stub) end new_name = "__simple_stub__#{method_name}" @stubs[key] = Stub.new(object, method_name, new_name) object.singleton_class.send :alias_method, new_name, method_name object.define_singleton_method(method_name, &block) end
def unstub_all!
def unstub_all! @stubs.each_value do |stub| unstub_object(stub) end @stubs = {} end
def unstub_object(stub)
def unstub_object(stub) singleton_class = stub.object.singleton_class singleton_class.send :undef_method, stub.method_name singleton_class.send :alias_method, stub.method_name, stub.original_method singleton_class.send :undef_method, stub.original_method end