aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/testing/time_helpers.rb
Commit message (Collapse)AuthorAgeFilesLines
* Enable `Layout/EmptyLinesAroundAccessModifier` copRyuta Kamizono2019-06-131-2/+0
| | | | | | | | | | | We sometimes say "✂️ newline after `private`" in a code review (e.g. https://github.com/rails/rails/pull/18546#discussion_r23188776, https://github.com/rails/rails/pull/34832#discussion_r244847195). Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style `EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059). That cop and enforced style will reduce the our code review cost.
* Module#{define_method,alias_method,undef_method,remove_method} become public ↵Ryuta Kamizono2018-12-211-4/+4
| | | | | | since Ruby 2.5 https://bugs.ruby-lang.org/issues/14133
* Oxford comma [ci skip]Xavier Noria2018-09-101-1/+1
| | | | | Followup to a recently merged PR, too minor and detailed to require a modification to the contributed patch.
* Add #unfreeze_time to ActiveSupport::Testing::TimeHelpersryanwhocodes2018-09-101-1/+2
|
* Remove usage of strip_heredoc in the framework in favor of <<~Rafael Mendonça França2018-02-161-2/+1
| | | | | Some places we can't remove because Ruby still don't have a method equivalent to strip_heredoc to be called in an already existent string.
* Prevent race condition when resetting time stubsEugene Kenny2017-12-121-1/+2
| | | | | | | | | If the current thread is preempted after the stub has been removed but before the original method has been restored, then the other thread will get a `NoMethodError` when it tries to call the method. Using `silence_redefinition_of_method` instead of `undef_method` ensures that either the stub or the original method is always in place.
* [Active Support] require_relative => requireAkira Matsuda2017-10-211-2/+2
| | | | This basically reverts 8da30ad6be34339124ba4cb4e36aea260dda12bc
* Merge pull request #29860 from georgeclaghorn/travel-back-automaticallyRafael França2017-07-241-1/+8
|\ | | | | Remove time stubs after each test
| * Remove time stubs after each testGeorge Claghorn2017-07-221-1/+8
| | | | | | | | Reverts 7abb6e0.
* | Fix doc format for `ActiveSupport::Testing::TimeHelpers` [ci skip]yuuji.yaginuma2017-07-151-2/+2
|/
* [Active Support] `rubocop -a --only Layout/EmptyLineAfterMagicComment`Koichi ITO2017-07-111-0/+1
|
* :scissors:Ryuta Kamizono2017-07-111-1/+0
| | | | [ci skip]
* Added time helper method `freeze_time` which is an alias for `travel_to ↵प्रथमेश Sonpatki2017-07-101-0/+21
| | | | Time.now` (#29681)
* Use frozen-string-literal in ActiveSupportKir Shatrov2017-07-091-0/+1
|
* [Active Support] require => require_relativeAkira Matsuda2017-07-011-2/+2
|
* Explicitly require AS::Time in AS::Testing::TimeHelpersT.J. Schuck2017-04-181-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If you just try to use `ActiveSupport::Testing::TimeHelpers` standalone by requiring `active_support/testing/time_helpers`, you currently get an error: `NoMethodError: undefined method `change' for 2017-12-14 01:04:44 -0500:Time` 9f6e82ee4783e491c20f5244a613fdeb4024beb5 added a dependency on `AS::Time` by using `AS::Time#change`. Here's a script to reproduce the error: ```ruby require "bundler/inline" gemfile(true) do source "https://rubygems.org" gem "activesupport", github: "rails/rails" end require "active_support/testing/time_helpers" require "minitest/autorun" class BugTest < Minitest::Test include ActiveSupport::Testing::TimeHelpers def test_stuff travel_to Time.new(2017, 12, 14, 01, 04, 44) do assert true end end end ``` It currently fails for all 5.x.x versions and master. Ideally, this would be backported to `5-0-stable` and `5-1-stable` as well.
* Make time travel work with subclasses of Time/Date/DatetimeJonas Nicklas2017-01-101-5/+5
| | | | | 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.
* applies new string literal convention in activesupport/libXavier Noria2016-08-061-2/+2
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* `travel/travel_to` travel time helpers, now raise on nested calls,Vipul A M2016-07-021-8/+43
| | | | | | | | | | | | | | | | | | | | | | | | | 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
* Create times in rails timezone not system timezone [ci skip]aarongray2016-06-211-3/+3
| | | | | | Time.new is a Ruby method that uses system timezone. Traveling in time using it is a recipe for confusion. Instead, Time.zone.local should be used since it uses the Rails timezone.
* A few documentation tweaks [ci skip]Robin Dupret2015-06-071-1/+1
| | | | [Robin Dupret & Shunsuke Aida]
* Small doc fix. [CI SKIP]Guo Xiang Tan2015-03-241-1/+1
|
* add `DateTime.now` to list of `TimeHelpers#travel_to` stubbing [ci skip]yuuji.yaginuma2015-03-121-9/+11
|
* Change AS::Testing::TimeHelpers#travel_to to also stub DateTime.nowYuki Nishijima2015-02-031-0/+1
|
* Merge pull request #17302 from ↵Rafael Mendonça França2014-10-181-2/+2
| | | | | | claudiob/replace-slower-block-call-with-faster-yield Replace (slower) block.call with (faster) yield
* Revert "Replace (slower) block.call with (faster) yield"Zachary Scott2014-10-181-2/+2
| | | | This reverts commit 0ab075e75f58bf403f7ebe20546c7005f35db1f6.
* Replace (slower) block.call with (faster) yieldclaudiob2014-10-181-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Performance optimization: `yield` with an implicit `block` is faster than `block.call`. See http://youtu.be/fGFM_UrSp70?t=10m35s and the following benchmark: ```ruby require 'benchmark/ips' def fast yield end def slow(&block) block.call end Benchmark.ips do |x| x.report('fast') { fast{} } x.report('slow') { slow{} } end # => fast 154095 i/100ms # => slow 71454 i/100ms # => # => fast 7511067.8 (±5.0%) i/s - 37445085 in 4.999660s # => slow 1227576.9 (±6.8%) i/s - 6145044 in 5.028356s ```
* Fix rounding errors with #travel_to by resetting the usec on any passed time ↵David Heinemeier Hansson2014-08-171-1/+5
| | | | to zero, so we only travel with per-second precision, not anything deeper than that.
* travel_to travels back and re-raises if the block raisesXavier Noria2014-02-181-3/+6
|
* time helpers honor the application time zone when passed a dateXavier Noria2014-02-181-4/+19
| | | | | | | | | | | | | | | | | Rails applications are expected to be always aware of the application time zone. To be consistent with that contract, we have to assume that a bare date passed to time helpers is a date in the application time zone, not in the system time zone. The system time zone is irrelevant, we should totally ignore it. For example, travel_to user.birth_date + 40.years should make that user be 40th years old regardless of the system time zone. Without this patch that may not be true.
* Improve documentation [ci skip]Rafael Mendonça França2014-01-301-3/+4
|
* Remove automatic removal of Date/Time stubs after each test caseRafael Mendonça França2014-01-301-9/+2
| | | | | | This behavior is only work out-of-box with minitest and also add a downside to run after each test case, even if we don't used the travel or travel_to methods
* Add `travel_back` to remove stubs from `travel` and `travel_to`Rafael Mendonça França2014-01-301-3/+14
|
* Use each_valueRafael Mendonça França2014-01-291-1/+1
|
* Store the singleton_class in a local variableRafael Mendonça França2014-01-291-3/+4
|
* Change the class and method visibilityRafael Mendonça França2014-01-291-9/+13
|
* Use instance method instead of before hookRafael Mendonça França2014-01-291-9/+8
|
* Alias the original method first to avoid warningsRafael Mendonça França2014-01-291-2/+7
|
* Implement a simple stub feature to use in the Time travel helpersRafael Mendonça França2014-01-291-4/+44
|
* doc `travel` and `travel_to` stubs are removed in teardown. [ci skip]Yves Senn2013-11-211-4/+4
|
* Add `#travel` and `#travel_to` to AS::TestCasePrem Sichanugrist2013-11-201-0/+55
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.