aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/testing/time_helpers.rb
diff options
context:
space:
mode:
authorJonas Nicklas <jonas.nicklas@gmail.com>2017-01-09 11:24:53 +0100
committerJonas Nicklas <jonas.nicklas@gmail.com>2017-01-10 11:43:26 +0100
commit3d78949c7451858231cce883411a7ac2595f5780 (patch)
tree28a6ec957ccf2285acb49006a646fd4e1b808fe5 /activesupport/lib/active_support/testing/time_helpers.rb
parent7f19f30819dd5a5a788ad9d8b12b5dda76559a12 (diff)
downloadrails-3d78949c7451858231cce883411a7ac2595f5780.tar.gz
rails-3d78949c7451858231cce883411a7ac2595f5780.tar.bz2
rails-3d78949c7451858231cce883411a7ac2595f5780.zip
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.
Diffstat (limited to 'activesupport/lib/active_support/testing/time_helpers.rb')
-rw-r--r--activesupport/lib/active_support/testing/time_helpers.rb10
1 files changed, 5 insertions, 5 deletions
diff --git a/activesupport/lib/active_support/testing/time_helpers.rb b/activesupport/lib/active_support/testing/time_helpers.rb
index e2f008b4b7..07c9be0604 100644
--- a/activesupport/lib/active_support/testing/time_helpers.rb
+++ b/activesupport/lib/active_support/testing/time_helpers.rb
@@ -10,7 +10,7 @@ module ActiveSupport
@stubs = Concurrent::Map.new { |h, k| h[k] = {} }
end
- def stub_object(object, method_name, return_value)
+ def stub_object(object, method_name, &block)
if stub = stubbing(object, method_name)
unstub_object(stub)
end
@@ -20,7 +20,7 @@ module ActiveSupport
@stubs[object.object_id][method_name] = Stub.new(object, method_name, new_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
def unstub_all!
@@ -134,9 +134,9 @@ module ActiveSupport
now = date_or_time.to_time.change(usec: 0)
end
- simple_stubs.stub_object(Time, :now, now)
- simple_stubs.stub_object(Date, :today, now.to_date)
- simple_stubs.stub_object(DateTime, :now, now.to_datetime)
+ simple_stubs.stub_object(Time, :now) { at(now.to_i) }
+ simple_stubs.stub_object(Date, :today) { jd(now.to_date.jd) }
+ 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?
begin