From 41b469d0995dd482f3fd4dfbae80770df0479883 Mon Sep 17 00:00:00 2001 From: Gaurish Sharma Date: Mon, 9 Nov 2015 18:07:10 +0530 Subject: :nailcare: Grammer fixes --- guides/source/testing.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'guides') diff --git a/guides/source/testing.md b/guides/source/testing.md index a07772036b..89ae3f83e3 100644 --- a/guides/source/testing.md +++ b/guides/source/testing.md @@ -155,7 +155,7 @@ Failed assertion, no message given. 1 tests, 1 assertions, 1 failures, 0 errors, 0 skips ``` -In the output, `F` denotes a failure. You can see the corresponding trace shown under `1)` along with the name of the failing test. The next few lines contain the stack trace followed by a message which mentions the actual value and the expected value by the assertion. The default assertion messages provide just enough information to help pinpoint the error. To make the assertion failure message more readable, every assertion provides an optional message parameter, as shown here: +In the output, `F` denotes a failure. You can see the corresponding trace shown under `1)` along with the name of the failing test. The next few lines contain the stack trace followed by a message that mentions the actual value and the expected value by the assertion. The default assertion messages provide just enough information to help pinpoint the error. To make the assertion failure message more readable, every assertion provides an optional message parameter, as shown here: ```ruby test "should not save article without title" do @@ -523,7 +523,7 @@ Model tests don't have their own superclass like `ActionMailer::TestCase` instea Integration Testing ------------------- -Integration tests are used to test how various parts of your application interact. They are generally used to test important work flows within your application. +Integration tests are used to test how various parts of your application interact. They are generally used to test important workflows within your application. For creating Rails integration tests, we use the 'test/integration' directory for your application. Rails provides a generator to create an integration test skeleton for you. -- cgit v1.2.3 From 06adda5f3b7e622d08c1d87ec326d59df55e122f Mon Sep 17 00:00:00 2001 From: Gaurish Sharma Date: Mon, 9 Nov 2015 18:57:11 +0530 Subject: Document time testing helpers --- guides/CHANGELOG.md | 3 +++ guides/source/testing.md | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) (limited to 'guides') diff --git a/guides/CHANGELOG.md b/guides/CHANGELOG.md index 09fb7b1a0e..3179746165 100644 --- a/guides/CHANGELOG.md +++ b/guides/CHANGELOG.md @@ -1,3 +1,6 @@ +* New section in Testing Guide - Testing Time Sensitive Code + *Gaurish Sharma* + * Add code of conduct to contributing guide *Jon Moss* diff --git a/guides/source/testing.md b/guides/source/testing.md index 89ae3f83e3..ea832e7176 100644 --- a/guides/source/testing.md +++ b/guides/source/testing.md @@ -1232,3 +1232,31 @@ class ProductTest < ActiveJob::TestCase end 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 1month +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 + + +```ruby +user = User.create(name: 'Gaurish', activation_date: Date.new(2004, 10, 24)) +assert_not user.applicable_for_gifting? # `activation_date` => Wed, 24 Nov 2004 +travel_to Date.new(2004, 11, 24) do + assert_equal Date.new(2004, 10, 24), user.activation_date # inside the trave_to block `Date.current` is mocked + assert user.applicable_for_gifting? # `activation_date` => Sun, 24 Oct 2004 +end +assert_equal Date.new(2004, 10, 24), user.activation_date # Outside the block, changed are undone +``` + +Please see [`ActiveSupport::TimeHelpers` API Documentation](http://api.rubyonrails.org/classes/ActiveSupport/Testing/TimeHelpers.html) +for in-depth information about the available time helpers. + -- cgit v1.2.3 From a1ca4907df8a3bc0e1b684bf9d59e857d1e128b6 Mon Sep 17 00:00:00 2001 From: Gaurish Sharma Date: Sat, 14 Nov 2015 01:45:24 +0530 Subject: Grammer & Style Tweaks as per feedback --- guides/CHANGELOG.md | 3 ++- guides/source/testing.md | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'guides') diff --git a/guides/CHANGELOG.md b/guides/CHANGELOG.md index 3179746165..a58dfdcc37 100644 --- a/guides/CHANGELOG.md +++ b/guides/CHANGELOG.md @@ -1,4 +1,5 @@ -* New section in Testing Guide - Testing Time Sensitive Code +* New section in Testing Guide - Testing Time-Dependent Code + *Gaurish Sharma* * Add code of conduct to contributing guide diff --git a/guides/source/testing.md b/guides/source/testing.md index ea832e7176..ce9dd1f49d 100644 --- a/guides/source/testing.md +++ b/guides/source/testing.md @@ -1237,8 +1237,8 @@ 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 1month -which is calculated from their activation date. to test such business logic in your tests +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, @@ -1251,7 +1251,7 @@ Here is an example using [`travel_to`](http://api.rubyonrails.org/classes/Active user = User.create(name: 'Gaurish', activation_date: Date.new(2004, 10, 24)) assert_not user.applicable_for_gifting? # `activation_date` => Wed, 24 Nov 2004 travel_to Date.new(2004, 11, 24) do - assert_equal Date.new(2004, 10, 24), user.activation_date # inside the trave_to block `Date.current` is mocked + 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 end assert_equal Date.new(2004, 10, 24), user.activation_date # Outside the block, changed are undone -- cgit v1.2.3 From 98af6e3ba497574fd5bc561ae0b52a3fa40b0761 Mon Sep 17 00:00:00 2001 From: Gaurish Sharma Date: Sat, 14 Nov 2015 23:50:56 +0530 Subject: rollback changes to changelog --- guides/CHANGELOG.md | 4 ---- 1 file changed, 4 deletions(-) (limited to 'guides') diff --git a/guides/CHANGELOG.md b/guides/CHANGELOG.md index a58dfdcc37..09fb7b1a0e 100644 --- a/guides/CHANGELOG.md +++ b/guides/CHANGELOG.md @@ -1,7 +1,3 @@ -* New section in Testing Guide - Testing Time-Dependent Code - - *Gaurish Sharma* - * Add code of conduct to contributing guide *Jon Moss* -- cgit v1.2.3 From 905226fc262861201d843d622a803ed1aeaebaa8 Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Sun, 15 Nov 2015 19:16:16 +0000 Subject: copy edits [ci skip] --- guides/source/testing.md | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) (limited to 'guides') 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) -- cgit v1.2.3