1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Make time travel work with subclasses of Time/Date/Datetime

Closes #27614

Previously when calling `now` on a subclass of e.g. `Time` it would return an instance of `Time` instead of returning an instance of the subclass. This way, we always return the correct class.
This commit is contained in:
Jonas Nicklas 2017-01-09 11:24:53 +01:00
parent 7f19f30819
commit 3d78949c74
2 changed files with 24 additions and 5 deletions

View file

@ -10,7 +10,7 @@ module ActiveSupport
@stubs = Concurrent::Map.new { |h, k| h[k] = {} } @stubs = Concurrent::Map.new { |h, k| h[k] = {} }
end end
def stub_object(object, method_name, return_value) def stub_object(object, method_name, &block)
if stub = stubbing(object, method_name) if stub = stubbing(object, method_name)
unstub_object(stub) unstub_object(stub)
end end
@ -20,7 +20,7 @@ module ActiveSupport
@stubs[object.object_id][method_name] = Stub.new(object, method_name, new_name) @stubs[object.object_id][method_name] = Stub.new(object, method_name, new_name)
object.singleton_class.send :alias_method, new_name, method_name object.singleton_class.send :alias_method, new_name, method_name
object.define_singleton_method(method_name) { return_value } object.define_singleton_method(method_name, &block)
end end
def unstub_all! def unstub_all!
@ -134,9 +134,9 @@ module ActiveSupport
now = date_or_time.to_time.change(usec: 0) now = date_or_time.to_time.change(usec: 0)
end end
simple_stubs.stub_object(Time, :now, now) simple_stubs.stub_object(Time, :now) { at(now.to_i) }
simple_stubs.stub_object(Date, :today, now.to_date) simple_stubs.stub_object(Date, :today) { jd(now.to_date.jd) }
simple_stubs.stub_object(DateTime, :now, now.to_datetime) simple_stubs.stub_object(DateTime, :now) { jd(now.to_date.jd, now.hour, now.min, now.sec, Rational(now.utc_offset, 86400)) }
if block_given? if block_given?
begin begin

View file

@ -3,6 +3,10 @@ require "active_support/core_ext/date_time"
require "active_support/core_ext/numeric/time" require "active_support/core_ext/numeric/time"
class TimeTravelTest < ActiveSupport::TestCase class TimeTravelTest < ActiveSupport::TestCase
class TimeSubclass < ::Time; end
class DateSubclass < ::Date; end
class DateTimeSubclass < ::DateTime; end
def test_time_helper_travel def test_time_helper_travel
Time.stub(:now, Time.now) do Time.stub(:now, Time.now) do
begin begin
@ -142,4 +146,19 @@ class TimeTravelTest < ActiveSupport::TestCase
end end
end end
end end
def test_time_helper_travel_with_time_subclass
assert_equal TimeSubclass, TimeSubclass.now.class
assert_equal DateSubclass, DateSubclass.today.class
assert_equal DateTimeSubclass, DateTimeSubclass.now.class
travel 1.day do
assert_equal TimeSubclass, TimeSubclass.now.class
assert_equal DateSubclass, DateSubclass.today.class
assert_equal DateTimeSubclass, DateTimeSubclass.now.class
assert_equal Time.now.to_s, TimeSubclass.now.to_s
assert_equal Date.today.to_s, DateSubclass.today.to_s
assert_equal DateTime.now.to_s, DateTimeSubclass.now.to_s
end
end
end end