Permalink
Browse files

travel_to travels back and re-raises if the block raises

  • Loading branch information...
1 parent 24fa399 commit 5d037819ca80606638212f83de741cc2041db28f @fxn fxn committed Feb 18, 2014
Showing with 18 additions and 3 deletions.
  1. +6 −3 activesupport/lib/active_support/testing/time_helpers.rb
  2. +12 −0 activesupport/test/time_zone_test.rb
@@ -10,7 +10,7 @@ def initialize
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,8 +97,11 @@ def travel_to(date_or_time, &block)
simple_stubs.stub_object(Date, :today, now.to_date)
if block_given?
- block.call
- travel_back
+ begin
+ block.call
+ ensure
+ travel_back
+ end
end
end
@@ -138,6 +138,18 @@ def test_travel_to_a_date
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

0 comments on commit 5d03781

Please sign in to comment.