class ActiveSupport::Testing::SimpleStubs
:nodoc:
Manages stubs for TimeHelpers
def initialize
def initialize @stubs = Concurrent::Map.new { |h, k| h[k] = {} } end
def stub_object(object, method_name, &block)
simple_stubs.stub_object(Time, :now) { at(target.to_i) }
target = Time.zone.local(2004, 11, 24, 1, 4, 44)
Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
so that removing this stub will restore the original implementation.
If the method is already stubbed, remove that stub
Stubs object.method_name with the given block
def stub_object(object, method_name, &block) if stub = stubbing(object, method_name) unstub_object(stub) end new_name = "__simple_stub__#{method_name}" @stubs[object.object_id][method_name] = Stub.new(object, method_name, new_name) object.singleton_class.alias_method new_name, method_name object.define_singleton_method(method_name, &block) end
def stubbed?
def stubbed? !@stubs.empty? end
def stubbing(object, method_name)
Returns the Stub for object#method_name
def stubbing(object, method_name) @stubs[object.object_id][method_name] end
def unstub_all!
def unstub_all! @stubs.each_value do |object_stubs| object_stubs.each_value do |stub| unstub_object(stub) end end @stubs.clear end
def unstub_object(stub)
def unstub_object(stub) singleton_class = stub.object.singleton_class singleton_class.silence_redefinition_of_method stub.method_name singleton_class.alias_method stub.method_name, stub.original_method singleton_class.undef_method stub.original_method end