diff options
author | Vipul A M <vipulnsward@gmail.com> | 2016-05-06 11:39:32 -0500 |
---|---|---|
committer | Vipul A M <vipulnsward@gmail.com> | 2016-07-02 15:09:34 -0700 |
commit | 919e7053622422d4a653f43b57da5a4179d5bbb2 (patch) | |
tree | c210813493a325f8e66c2b42fdea7f4e70175d37 /activesupport/test | |
parent | 173bf3506d99c0767a41d30fbe4d306201369194 (diff) | |
download | rails-919e7053622422d4a653f43b57da5a4179d5bbb2.tar.gz rails-919e7053622422d4a653f43b57da5a4179d5bbb2.tar.bz2 rails-919e7053622422d4a653f43b57da5a4179d5bbb2.zip |
`travel/travel_to` travel time helpers, now raise on nested calls,
as this can lead to confusing time stubbing.
Instead of:
travel_to 2.days.from_now do
# 2 days from today
travel_to 3.days.from_now do
# 5 days from today
end
end
preferred way to achieve above is:
travel_to 2.days.from_now
# 2 days from today
travel_back
travel_to 5.days.from_now
# 5 days from today
Closes #24690
Fixes #24689
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/time_travel_test.rb | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/activesupport/test/time_travel_test.rb b/activesupport/test/time_travel_test.rb index 59c3e52c2f..e9f87bdf82 100644 --- a/activesupport/test/time_travel_test.rb +++ b/activesupport/test/time_travel_test.rb @@ -77,6 +77,49 @@ class TimeTravelTest < ActiveSupport::TestCase end end + def test_time_helper_travel_to_with_nested_calls_with_blocks + Time.stub(:now, Time.now) do + outer_expected_time = Time.new(2004, 11, 24, 01, 04, 44) + inner_expected_time = Time.new(2004, 10, 24, 01, 04, 44) + travel_to outer_expected_time do + assert_raises(RuntimeError, /Calling `travel_to` with a block, when we have previously already made a call to `travel_to`, can lead to confusing time stubbing./) do + travel_to(inner_expected_time) do + #noop + end + end + end + end + end + + def test_time_helper_travel_to_with_nested_calls + Time.stub(:now, Time.now) do + outer_expected_time = Time.new(2004, 11, 24, 01, 04, 44) + inner_expected_time = Time.new(2004, 10, 24, 01, 04, 44) + travel_to outer_expected_time do + assert_nothing_raised do + travel_to(inner_expected_time) + + assert_equal inner_expected_time, Time.now + end + end + end + end + + def test_time_helper_travel_to_with_subsequent_calls + Time.stub(:now, Time.now) do + initial_expected_time = Time.new(2004, 11, 24, 01, 04, 44) + subsequent_expected_time = Time.new(2004, 10, 24, 01, 04, 44) + assert_nothing_raised do + travel_to initial_expected_time + travel_to subsequent_expected_time + + assert_equal subsequent_expected_time, Time.now + + travel_back + end + end + end + def test_travel_to_will_reset_the_usec_to_avoid_mysql_rouding Time.stub(:now, Time.now) do travel_to Time.utc(2014, 10, 10, 10, 10, 50, 999999) do |