Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
b43b6d5c
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b43b6d5c
编写于
11月 20, 2013
作者:
X
Xavier Noria
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #12824 from sikachu/ps-testing-time-helper
Add `#travel` and `#travel_to` to `ActiveSupport::TestCase`
上级
1ffa4abc
225cd915
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
144 addition
and
2 deletion
+144
-2
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+39
-0
activesupport/lib/active_support/test_case.rb
activesupport/lib/active_support/test_case.rb
+2
-0
activesupport/lib/active_support/testing/time_helpers.rb
activesupport/lib/active_support/testing/time_helpers.rb
+55
-0
activesupport/test/test_test.rb
activesupport/test/test_test.rb
+48
-2
未找到文件。
activesupport/CHANGELOG.md
浏览文件 @
b43b6d5c
*
Add
`ActiveSupport::Testing::TimeHelpers#travel`
and
`#travel_to`
. These methods change current
time to the given time or time difference by stubbing
`Time.now`
and
`Date.today`
to return the
time or date after the difference calculation, or the time or date that got passed into the
method respectively. These methods also accept a block, which will return current time back to
its original state at the end of the block.
Example for `#travel`:
Time.now # => 2013-11-09 15:34:49 -05:00
travel 1.day
Time.now # => 2013-11-10 15:34:49 -05:00
Date.today # => Sun, 10 Nov 2013
Example for `#travel_to`:
Time.now # => 2013-11-09 15:34:49 -05:00
travel_to Time.new(2004, 11, 24, 01, 04, 44)
Time.now # => 2004-11-24 01:04:44 -05:00
Date.today # => Wed, 24 Nov 2004
Both of these methods also accept a block, which will return the current time back to its
original state at the end of the block:
Time.now # => 2013-11-09 15:34:49 -05:00
travel 1.day do
User.create.created_at # => Sun, 10 Nov 2013 15:34:49 EST -05:00
end
travel_to Time.new(2004, 11, 24, 01, 04, 44) do
User.create.created_at # => Wed, 24 Nov 2004 01:04:44 EST -05:00
end
Time.now # => 2013-11-09 15:34:49 -05:00
This module is included in `ActiveSupport::TestCase` automatically.
*Prem Sichanugrist*, *DHH*
*
Unify
`cattr_*`
interface: allow to pass a block to
`cattr_reader`
.
Example:
...
...
activesupport/lib/active_support/test_case.rb
浏览文件 @
b43b6d5c
...
...
@@ -7,6 +7,7 @@
require
'active_support/testing/declarative'
require
'active_support/testing/isolation'
require
'active_support/testing/constant_lookup'
require
'active_support/testing/time_helpers'
require
'active_support/core_ext/kernel/reporting'
require
'active_support/deprecation'
...
...
@@ -34,6 +35,7 @@ def self.for_tag(tag)
include
ActiveSupport
::
Testing
::
SetupAndTeardown
include
ActiveSupport
::
Testing
::
Assertions
include
ActiveSupport
::
Testing
::
Deprecation
include
ActiveSupport
::
Testing
::
TimeHelpers
extend
ActiveSupport
::
Testing
::
Declarative
# test/unit backwards compatibility methods
...
...
activesupport/lib/active_support/testing/time_helpers.rb
0 → 100644
浏览文件 @
b43b6d5c
module
ActiveSupport
module
Testing
# Containing helpers that helps you test passage of time.
module
TimeHelpers
# Change current time to the time in the future or in the past by a given time difference by
# stubbing +Time.now+ and +Date.today+. This method also accepts a block, which will return
# current time back to its original state at the end of the block.
#
# Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
# travel 1.day
# Time.current # => Sun, 10 Nov 2013 15:34:49 EST -05:00
# Date.current # => Sun, 10 Nov 2013
#
# This method also accepts a block, which will return the current time back to its original
# state at the end of the block:
#
# Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
# travel 1.day do
# User.create.created_at # => Sun, 10 Nov 2013 15:34:49 EST -05:00
# end
# Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
def
travel
(
duration
,
&
block
)
travel_to
Time
.
now
+
duration
,
&
block
end
# Change current time to the given time by stubbing +Time.now+ and +Date.today+ to return the
# time or date passed into this method. This method also accepts a block, which will return
# current time back to its original state at the end of the block.
#
# Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
# travel_to Time.new(2004, 11, 24, 01, 04, 44)
# Time.current # => Wed, 24 Nov 2004 01:04:44 EST -05:00
# Date.current # => Wed, 24 Nov 2004
#
# This method also accepts a block, which will return the current time back to its original
# state at the end of the block:
#
# Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
# travel_to Time.new(2004, 11, 24, 01, 04, 44) do
# User.create.created_at # => Wed, 24 Nov 2004 01:04:44 EST -05:00
# end
# Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
def
travel_to
(
date_or_time
,
&
block
)
Time
.
stubs
now:
date_or_time
.
to_time
Date
.
stubs
today:
date_or_time
.
to_date
if
block_given?
block
.
call
Time
.
unstub
:now
Date
.
unstub
:today
end
end
end
end
end
activesupport/test/test_test.rb
浏览文件 @
b43b6d5c
require
'abstract_unit'
require
'active_support/core_ext/date'
require
'active_support/core_ext/numeric/time'
class
AssertDifferenceTest
<
ActiveSupport
::
TestCase
def
setup
...
...
@@ -122,7 +124,6 @@ def sentinel
end
end
class
SubclassSetupAndTeardownTest
<
SetupAndTeardownTest
setup
:bar
teardown
:bar
...
...
@@ -143,7 +144,6 @@ def sentinel
end
end
class
TestCaseTaggedLoggingTest
<
ActiveSupport
::
TestCase
def
before_setup
require
'stringio'
...
...
@@ -156,3 +156,49 @@ def test_logs_tagged_with_current_test_case
assert_match
"
#{
self
.
class
}
:
#{
name
}
\n
"
,
@out
.
string
end
end
class
TimeHelperTest
<
ActiveSupport
::
TestCase
setup
do
Time
.
stubs
now:
Time
.
now
end
def
test_time_helper_travel
expected_time
=
Time
.
now
+
1
.
day
travel
1
.
day
assert_equal
expected_time
,
Time
.
now
assert_equal
expected_time
.
to_date
,
Date
.
today
end
def
test_time_helper_travel_with_block
expected_time
=
Time
.
now
+
1
.
day
travel
1
.
day
do
assert_equal
expected_time
,
Time
.
now
assert_equal
expected_time
.
to_date
,
Date
.
today
end
assert_not_equal
expected_time
,
Time
.
now
assert_not_equal
expected_time
.
to_date
,
Date
.
today
end
def
test_time_helper_travel_to
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
end
def
test_time_helper_travel_to_with_block
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
end
assert_not_equal
expected_time
,
Time
.
now
assert_not_equal
Date
.
new
(
2004
,
11
,
24
),
Date
.
today
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录