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

travel_to travels back and re-raises if the block raises

This commit is contained in:
Xavier Noria 2014-02-18 10:22:43 +01:00
parent 24fa399c99
commit 5d037819ca
2 changed files with 18 additions and 3 deletions

View file

@ -10,7 +10,7 @@ module ActiveSupport
def stub_object(object, method_name, return_value)
key = [object.object_id, method_name]
if (stub = @stubs[key])
if stub = @stubs[key]
unstub_object(stub)
end
@ -97,10 +97,13 @@ module ActiveSupport
simple_stubs.stub_object(Date, :today, now.to_date)
if block_given?
begin
block.call
ensure
travel_back
end
end
end
# Returns the current time back to its original state, by removing the stubs added by
# `travel` and `travel_to`.

View file

@ -138,6 +138,18 @@ class TimeZoneTest < ActiveSupport::TestCase
end
end
def test_travel_to_travels_back_and_reraises_if_the_block_raises
ts = Time.current - 1.second
travel_to ts do
raise
end
flunk # ensure travel_to re-raises
rescue
assert_not_equal ts, Time.current
end
def test_local
time = ActiveSupport::TimeZone["Hawaii"].local(2007, 2, 5, 15, 30, 45)
assert_equal Time.utc(2007, 2, 5, 15, 30, 45), time.time