aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
authorVijay Dev <vijaydev.cse@gmail.com>2015-11-15 19:16:16 +0000
committerVijay Dev <vijaydev.cse@gmail.com>2015-11-15 19:16:16 +0000
commit905226fc262861201d843d622a803ed1aeaebaa8 (patch)
tree0d3f350c50f168762c510b731e5a7ff006e8d3e6 /guides
parent29d0350a2db61cd2832cc34c4db72a6f62c2f48f (diff)
downloadrails-905226fc262861201d843d622a803ed1aeaebaa8.tar.gz
rails-905226fc262861201d843d622a803ed1aeaebaa8.tar.bz2
rails-905226fc262861201d843d622a803ed1aeaebaa8.zip
copy edits [ci skip]
Diffstat (limited to 'guides')
-rw-r--r--guides/source/testing.md18
1 files changed, 6 insertions, 12 deletions
diff --git a/guides/source/testing.md b/guides/source/testing.md
index ce9dd1f49d..97c642741a 100644
--- a/guides/source/testing.md
+++ b/guides/source/testing.md
@@ -1236,25 +1236,19 @@ end
Testing Time-Dependent Code
---------------------------
-You might have code in your rails app, that is time sensitive. For example,
-you want to give some gifts to users but only after they have been a member for month
-which is calculated from their activation date. To test such business logic in your tests
-you will need to time travel in your tests.
-
-Fortunately, Rails provides inbuild helper methods which allow you time travel in your test code,
-allowing you assert that your time-sensitve code works as expected.
-
-Here is an example using [`travel_to`](http://api.rubyonrails.org/classes/ActiveSupport/Testing/TimeHelpers.html#method-i-travel_to) helper
+Rails provides inbuilt helper methods that enable you to assert that your time-sensitve code works as expected.
+Here is an example using the [`travel_to`](http://api.rubyonrails.org/classes/ActiveSupport/Testing/TimeHelpers.html#method-i-travel_to) helper:
```ruby
+# Lets say that a user is eligible for gifting a month after they register.
user = User.create(name: 'Gaurish', activation_date: Date.new(2004, 10, 24))
-assert_not user.applicable_for_gifting? # `activation_date` => Wed, 24 Nov 2004
+assert_not user.applicable_for_gifting?
travel_to Date.new(2004, 11, 24) do
assert_equal Date.new(2004, 10, 24), user.activation_date # inside the travel_to block `Date.current` is mocked
- assert user.applicable_for_gifting? # `activation_date` => Sun, 24 Oct 2004
+ assert user.applicable_for_gifting?
end
-assert_equal Date.new(2004, 10, 24), user.activation_date # Outside the block, changed are undone
+assert_equal Date.new(2004, 10, 24), user.activation_date # The change was visible only inside the `travel_to` block.
```
Please see [`ActiveSupport::TimeHelpers` API Documentation](http://api.rubyonrails.org/classes/ActiveSupport/Testing/TimeHelpers.html)