Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 86 lines (57 sloc) 3.393 kB
519e585 @tjschuck Update README
tjschuck authored
1 ## time-warp
d9cb382 @bjhess Change README file to markdown.
bjhess authored
2
3 When writing tests, it is often desirable to bend time in order to test limits and edges of the day. It is especially useful to warp time to test results across the timezones of the world. Manipulating time is also useful to assure a day of the week, month or year every time the test runs.
4
519e585 @tjschuck Update README
tjschuck authored
5 Some may say "Why not just mock `Time#now`?" I see the point, but I find mocking around with baseline Ruby classes to be asking for trouble. Eventually unusual behavior will rear its head and a day will be lost debugging tests - the most excruciating debugging one can be subjected to.
d9cb382 @bjhess Change README file to markdown.
bjhess authored
6
7
519e585 @tjschuck Update README
tjschuck authored
8 ## Installation
d9cb382 @bjhess Change README file to markdown.
bjhess authored
9
10 Plugin:
11
2d4a26c @bjhess Update README.
bjhess authored
12 $ script/plugin install git://github.com/harvesthq/time-warp.git
d9cb382 @bjhess Change README file to markdown.
bjhess authored
13
14 Gem:
15
519e585 @tjschuck Update README
tjschuck authored
16 $ gem install time-warp
d9cb382 @bjhess Change README file to markdown.
bjhess authored
17
519e585 @tjschuck Update README
tjschuck authored
18 Gem config for a Rails app in `config/environment.rb`:
d9cb382 @bjhess Change README file to markdown.
bjhess authored
19
2d4a26c @bjhess Update README.
bjhess authored
20 config.gem 'time-warp'
d9cb382 @bjhess Change README file to markdown.
bjhess authored
21
8e47ea2 @tjschuck Update README.md
tjschuck authored
22 ## Example
d9cb382 @bjhess Change README file to markdown.
bjhess authored
23
519e585 @tjschuck Update README
tjschuck authored
24 And now, a contrived example. In this case, the goal is to let the full mechanics of Rails execute. Yes, this test will even hit the database! The goal is to assure a particular day of week when each test method executes:
d9cb382 @bjhess Change README file to markdown.
bjhess authored
25
26 require File.dirname(__FILE__) + '/../test_helper'
27 class CompanyTest < Test::Unit::TestCase
28
29 def setup
30 @company = companies(:acme)
31 end
32
33 def test_should_find_company_needing_reminded_today
34 pretend_now_is(Time.utc(2008,"jul",24,20)) do #=> Thu Jul 24 20:00:00 UTC 2008
35 @company.reminder_day = 'Thursday'
36 @company.save
37 companies = Company.find_companies_needing_reminded_today
38 assert_equal true, companies.include?(@company)
39 end
40 end
41
42 def test_should_not_find_company_needing_reminded_tomorrow
43 pretend_now_is(Time.utc(2008,"jul",24,20)) do #=> Thu Jul 24 20:00:00 UTC 2008
44 @company.reminder_day = 'Friday'
45 @company.save
46 companies = Company.find_companies_needing_reminded_today
47 assert_equal false, companies.include?(@company)
48 end
49 end
50
51 def test_should_not_find_company_needing_reminded_yesterday
52 pretend_now_is(Time.utc(2008,"jul",24,20)) do #=> Thu Jul 24 20:00:00 UTC 2008
53 @company.reminder_day = 'Wednesday'
54 @company.save
55 companies = Company.find_companies_needing_reminded_today
56 assert_equal false, companies.include?(@company)
57 end
58 end
59 end
60
8e47ea2 @tjschuck Update README.md
tjschuck authored
61 ## Notes
d9cb382 @bjhess Change README file to markdown.
bjhess authored
62
519e585 @tjschuck Update README
tjschuck authored
63 The `pretend_now_is` method may also be called with the arguments for the `Time#utc` call, rather than a `Time` argument. So:
d9cb382 @bjhess Change README file to markdown.
bjhess authored
64
65 pretend_now_is(Time.utc(2008,"jul",24,20)) do
66 # Shifted code
67 end
68
69 Becomes:
70
71 pretend_now_is(2008,"jul",24,20) do
72 # Shifted code
73 end
74
519e585 @tjschuck Update README
tjschuck authored
75 Also, `pretend_now_is` should impact `ActiveSupport` generated `Date` extensions such as `Date.today`, `Date.tomorrow`, and so on.
d9cb382 @bjhess Change README file to markdown.
bjhess authored
76
8e47ea2 @tjschuck Update README.md
tjschuck authored
77 ## Credits
d9cb382 @bjhess Change README file to markdown.
bjhess authored
78
519e585 @tjschuck Update README
tjschuck authored
79 time-warp is maintained and funded by [Harvest](http://www.getHarvest.com). Want to work on projects like this? [We're hiring](http://www.getharvest.com/careers)!
80
d9cb382 @bjhess Change README file to markdown.
bjhess authored
81 The creation of this plugin is a direct result of Jason M. Felice's snippet (and ensuing discussion). The snippet can be found [at DZone](http://snippets.dzone.com/posts/show/1738).
82
83 Further discussion of this snippet's evolution may be found [at Barry Hess's blog](http://bjhess.com/blog/2007/08/12/time-warp-for-rails-testing/).
84
e080d02 @bjhess More README updates for Harvest links rather than Iridesco.
bjhess authored
85 Copyright (c) 2008 [Barry Hess](http://bjhess.com), [Harvest](http://www.getHarvest.com). Released under the MIT license.
Something went wrong with that request. Please try again.