aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/test_case.rb
diff options
context:
space:
mode:
authorPrem Sichanugrist <s@sikac.hu>2013-11-09 13:03:28 -0500
committerPrem Sichanugrist <s@sikac.hu>2013-11-20 16:05:02 +0700
commit225cd915cf5ab3b6662d3eecf3fe0715bf73e4de (patch)
tree4249462334353ee711378c335f95df3655d82f42 /activesupport/lib/active_support/test_case.rb
parent1ffa4abcd595b98ad83bc6b80cdf3b5a30fa25c9 (diff)
downloadrails-225cd915cf5ab3b6662d3eecf3fe0715bf73e4de.tar.gz
rails-225cd915cf5ab3b6662d3eecf3fe0715bf73e4de.tar.bz2
rails-225cd915cf5ab3b6662d3eecf3fe0715bf73e4de.zip
Add `#travel` and `#travel_to` to AS::TestCase
Add `ActiveSupport::Testing::TimeHelpers#travel` and `#travel_to`. These methods change current time to the given time or time difference by stubbing `Time.now` and `Date.today` to return the time or date after the difference calculation, or the time or date that got passed into the method respectively. These methods also accept a block, which will return current time back to its original state at the end of the block. Example for `#travel`: Time.now # => 2013-11-09 15:34:49 -05:00 travel 1.day Time.now # => 2013-11-10 15:34:49 -05:00 Date.today # => Sun, 10 Nov 2013 Example for `#travel_to`: Time.now # => 2013-11-09 15:34:49 -05:00 travel_to Time.new(2004, 11, 24, 01, 04, 44) Time.now # => 2004-11-24 01:04:44 -05:00 Date.today # => Wed, 24 Nov 2004 Both of these methods also accept a block, which will return the current time back to its original state at the end of the block: Time.now # => 2013-11-09 15:34:49 -05:00 travel 1.day do User.create.created_at # => Sun, 10 Nov 2013 15:34:49 EST -05:00 end travel_to Time.new(2004, 11, 24, 01, 04, 44) do User.create.created_at # => Wed, 24 Nov 2004 01:04:44 EST -05:00 end Time.now # => 2013-11-09 15:34:49 -05:00 This module is included in `ActiveSupport::TestCase` automatically.
Diffstat (limited to 'activesupport/lib/active_support/test_case.rb')
-rw-r--r--activesupport/lib/active_support/test_case.rb2
1 files changed, 2 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb
index e193163d17..2fb5c04316 100644
--- a/activesupport/lib/active_support/test_case.rb
+++ b/activesupport/lib/active_support/test_case.rb
@@ -7,6 +7,7 @@ require 'active_support/testing/deprecation'
require 'active_support/testing/declarative'
require 'active_support/testing/isolation'
require 'active_support/testing/constant_lookup'
+require 'active_support/testing/time_helpers'
require 'active_support/core_ext/kernel/reporting'
require 'active_support/deprecation'
@@ -34,6 +35,7 @@ module ActiveSupport
include ActiveSupport::Testing::SetupAndTeardown
include ActiveSupport::Testing::Assertions
include ActiveSupport::Testing::Deprecation
+ include ActiveSupport::Testing::TimeHelpers
extend ActiveSupport::Testing::Declarative
# test/unit backwards compatibility methods