1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
require "abstract_unit"
require "active_support/core_ext/date_time"
require "active_support/core_ext/numeric/time"
class TimeTravelTest < ActiveSupport::TestCase
def test_time_helper_travel
Time.stub(:now, Time.now) do
begin
expected_time = Time.now + 1.day
travel 1.day
assert_equal expected_time.to_s(:db), Time.now.to_s(:db)
assert_equal expected_time.to_date, Date.today
assert_equal expected_time.to_datetime.to_s(:db), DateTime.now.to_s(:db)
ensure
travel_back
end
end
end
def test_time_helper_travel_with_block
Time.stub(:now, Time.now) do
expected_time = Time.now + 1.day
travel 1.day do
assert_equal expected_time.to_s(:db), Time.now.to_s(:db)
assert_equal expected_time.to_date, Date.today
assert_equal expected_time.to_datetime.to_s(:db), DateTime.now.to_s(:db)
end
assert_not_equal expected_time.to_s(:db), Time.now.to_s(:db)
assert_not_equal expected_time.to_date, Date.today
assert_not_equal expected_time.to_datetime.to_s(:db), DateTime.now.to_s(:db)
end
end
def test_time_helper_travel_to
Time.stub(:now, Time.now) do
begin
expected_time = Time.new(2004, 11, 24, 01, 04, 44)
travel_to expected_time
assert_equal expected_time, Time.now
assert_equal Date.new(2004, 11, 24), Date.today
assert_equal expected_time.to_datetime, DateTime.now
ensure
travel_back
end
end
end
def test_time_helper_travel_to_with_block
Time.stub(:now, Time.now) do
expected_time = Time.new(2004, 11, 24, 01, 04, 44)
travel_to expected_time do
assert_equal expected_time, Time.now
assert_equal Date.new(2004, 11, 24), Date.today
assert_equal expected_time.to_datetime, DateTime.now
end
assert_not_equal expected_time, Time.now
assert_not_equal Date.new(2004, 11, 24), Date.today
assert_not_equal expected_time.to_datetime, DateTime.now
end
end
def test_time_helper_travel_back
Time.stub(:now, Time.now) do
begin
expected_time = Time.new(2004, 11, 24, 01, 04, 44)
travel_to expected_time
assert_equal expected_time, Time.now
assert_equal Date.new(2004, 11, 24), Date.today
assert_equal expected_time.to_datetime, DateTime.now
travel_back
assert_not_equal expected_time, Time.now
assert_not_equal Date.new(2004, 11, 24), Date.today
assert_not_equal expected_time.to_datetime, DateTime.now
ensure
travel_back
end
end
end
def test_time_helper_travel_to_with_nested_calls_with_blocks
Time.stub(:now, Time.now) do
outer_expected_time = Time.new(2004, 11, 24, 01, 04, 44)
inner_expected_time = Time.new(2004, 10, 24, 01, 04, 44)
travel_to outer_expected_time do
assert_raises(RuntimeError, /Calling `travel_to` with a block, when we have previously already made a call to `travel_to`, can lead to confusing time stubbing./) do
travel_to(inner_expected_time) do
#noop
end
end
end
end
end
def test_time_helper_travel_to_with_nested_calls
Time.stub(:now, Time.now) do
outer_expected_time = Time.new(2004, 11, 24, 01, 04, 44)
inner_expected_time = Time.new(2004, 10, 24, 01, 04, 44)
travel_to outer_expected_time do
assert_nothing_raised do
travel_to(inner_expected_time)
assert_equal inner_expected_time, Time.now
end
end
end
end
def test_time_helper_travel_to_with_subsequent_calls
Time.stub(:now, Time.now) do
begin
initial_expected_time = Time.new(2004, 11, 24, 01, 04, 44)
subsequent_expected_time = Time.new(2004, 10, 24, 01, 04, 44)
assert_nothing_raised do
travel_to initial_expected_time
travel_to subsequent_expected_time
assert_equal subsequent_expected_time, Time.now
travel_back
end
ensure
travel_back
end
end
end
def test_travel_to_will_reset_the_usec_to_avoid_mysql_rouding
Time.stub(:now, Time.now) do
travel_to Time.utc(2014, 10, 10, 10, 10, 50, 999999) do
assert_equal 50, Time.now.sec
assert_equal 0, Time.now.usec
assert_equal 50, DateTime.now.sec
assert_equal 0, DateTime.now.usec
end
end
end
end
|