time-warp 1.0.10 → 1.0.11
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +13 -15
- data/lib/time_warp.rb +36 -28
- metadata +5 -5
data/README.md
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
|
1
|
+
## time-warp
|
2
2
|
|
3
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
4
|
|
5
|
-
Some may say "Why not just mock Time#now
|
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.
|
6
6
|
|
7
7
|
|
8
|
-
|
8
|
+
## Installation
|
9
9
|
|
10
10
|
Plugin:
|
11
11
|
|
@@ -13,15 +13,15 @@ Plugin:
|
|
13
13
|
|
14
14
|
Gem:
|
15
15
|
|
16
|
-
$
|
16
|
+
$ gem install time-warp
|
17
17
|
|
18
|
-
Gem config
|
18
|
+
Gem config for a Rails app in `config/environment.rb`:
|
19
19
|
|
20
20
|
config.gem 'time-warp'
|
21
21
|
|
22
|
-
|
22
|
+
## Example
|
23
23
|
|
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:
|
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:
|
25
25
|
|
26
26
|
require File.dirname(__FILE__) + '/../test_helper'
|
27
27
|
class CompanyTest < Test::Unit::TestCase
|
@@ -58,9 +58,9 @@ And now a contrived example. In this case, the goal is to let the full mechanic
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
|
-
|
61
|
+
## Notes
|
62
62
|
|
63
|
-
The
|
63
|
+
The `pretend_now_is` method may also be called with the arguments for the `Time#utc` call, rather than a `Time` argument. So:
|
64
64
|
|
65
65
|
pretend_now_is(Time.utc(2008,"jul",24,20)) do
|
66
66
|
# Shifted code
|
@@ -72,16 +72,14 @@ Becomes:
|
|
72
72
|
# Shifted code
|
73
73
|
end
|
74
74
|
|
75
|
-
Also,
|
75
|
+
Also, `pretend_now_is` should impact `ActiveSupport` generated `Date` extensions such as `Date.today`, `Date.tomorrow`, and so on.
|
76
76
|
|
77
|
-
Credits
|
78
|
-
|
77
|
+
## Credits
|
78
|
+
|
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)!
|
79
80
|
|
80
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).
|
81
82
|
|
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/).
|
83
84
|
|
84
|
-
time_warp is maintained and funded by [Harvest](http://www.getHarvest.com).
|
85
|
-
|
86
|
-
|
87
85
|
Copyright (c) 2008 [Barry Hess](http://bjhess.com), [Harvest](http://www.getHarvest.com). Released under the MIT license.
|
data/lib/time_warp.rb
CHANGED
@@ -1,36 +1,44 @@
|
|
1
1
|
require File.join File.dirname(__FILE__), 'core_ext'
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
3
|
+
module TimeWarpAbility
|
4
|
+
|
5
|
+
def reset_to_real_time
|
6
|
+
Time.testing_offset = 0
|
7
|
+
end
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
ensure
|
15
|
-
reset_to_real_time
|
16
|
-
end
|
9
|
+
def pretend_now_is(*args)
|
10
|
+
Time.testing_offset = Time.now - time_from(*args)
|
11
|
+
if block_given?
|
12
|
+
begin
|
13
|
+
yield
|
14
|
+
ensure
|
15
|
+
reset_to_real_time
|
17
16
|
end
|
18
17
|
end
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def time_from(*args)
|
23
|
+
return args[0] if 1 == args.size && args[0].is_a?(Time)
|
24
|
+
return args[0].to_time if 1 == args.size && args[0].respond_to?(:to_time) # For example, if it's a Date.
|
25
|
+
Time.utc(*args)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module Test # :nodoc:
|
30
|
+
module Unit # :nodoc:
|
31
|
+
class TestCase
|
32
|
+
include ::TimeWarpAbility
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
33
36
|
|
37
|
+
module RSpec
|
38
|
+
module Core
|
39
|
+
class ExampleGroup
|
40
|
+
include ::TimeWarpAbility
|
41
|
+
# Time warp to the specified time for the duration of the passed block.
|
34
42
|
end
|
35
43
|
end
|
36
|
-
end
|
44
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time-warp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 1
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 11
|
10
|
+
version: 1.0.11
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Barry Hess
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2012-01-12 00:00:00 Z
|
19
19
|
dependencies: []
|
20
20
|
|
21
21
|
description: TimeWarp is a ruby library for manipulating times in automated tests.
|
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
68
|
requirements: []
|
69
69
|
|
70
70
|
rubyforge_project:
|
71
|
-
rubygems_version: 1.8.
|
71
|
+
rubygems_version: 1.8.6
|
72
72
|
signing_key:
|
73
73
|
specification_version: 3
|
74
74
|
summary: Warp time in your tests
|