黄瓜与时间警察
我正在尝试将 Cucumber 和 timecop 一起使用,并且 timecop 似乎并没有覆盖我的 transaction.rb 模型文件中的 Time.now 调用。
这是我的场景:
@stop @wip
Scenario: Displaying defaults when entering a form
Given I am on the new transaction page
And the time is Dec 31 2010, 9:00 UTC
Then the "Tran date" field should contain "31/12/2010"
我在 timecop_steps.rb 文件中有这些步骤
Given 'the time is $time' do |time|
Timecop.freeze Time.parse(time)
end
When '$time pass' do |time|
Timecop.travel future_time(time)
Given 'delayed jobs are run' # we use delayed jobs and have some that get scheduled in the future.
end
When 'time stands still' do
Timecop.freeze Time.now
end
然后在我的 transaction.rb 文件中我设置了默认值:
class Transaction < ActiveRecord::Base
def set_defaults
self.tran_date = Time.now.strftime("%d/%m/%Y")
end
end
当我运行我的场景时,我得到以下信息:
Scenario: Displaying defaults when entering a form # features/receipt_journal_new.feature:29
Given I am on the new transaction page
# features/step_definitions/web_steps.rb:45
And the time is Dec 31 2010, 9:00 UTC # features/step_definitions/timecop_steps.rb:1
Then the "Tran date" field should contain "31/12/2010" # features/step_definitions/web_steps.rb:142
expected: /31\/12\/2010/
got: "20/07/2011" (using =~)
Diff:
@@ -1,2 +1,2 @@
-/31\/12\/2010/
+20/07/2011
(RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/web_steps.rb:147:in `block (2 levels) in <top (required)>'
./features/step_definitions/web_steps.rb:30:in `with_scope'
./features/step_definitions/web_steps.rb:143:in `/^the "([^"]*)" field(?: within (.*))? should contain "([^"]*)"$/'
features/transaction_new.feature:32:in `Then the "Tran date" field should contain "31/12/2010"'
更新:
我什至尝试删除 Time.now 和这也不起作用:
Given 'the time is $time' do |time|
Time.stub!(:now).and_return("31/12/2010") #Time.parse(time))
# Timecop.freeze Time.parse(time)
end
更新2:
我尝试使用慢性以及使用Timecop.travel而不是Timecop.freeze,如本文中所示:
http://www.louismrose.me.uk/post/876230592/freezing -time-in-cucumber
Update3:
我已经在我的 rspec 模型测试中测试了 Timecop,它工作得很好。
I'm trying to use cucumber and timecop together and it's timecop doesn't seem to be overriding my Time.now call in my transaction.rb model file.
Here is my Scenario:
@stop @wip
Scenario: Displaying defaults when entering a form
Given I am on the new transaction page
And the time is Dec 31 2010, 9:00 UTC
Then the "Tran date" field should contain "31/12/2010"
I have these steps in a timecop_steps.rb file
Given 'the time is $time' do |time|
Timecop.freeze Time.parse(time)
end
When '$time pass' do |time|
Timecop.travel future_time(time)
Given 'delayed jobs are run' # we use delayed jobs and have some that get scheduled in the future.
end
When 'time stands still' do
Timecop.freeze Time.now
end
Then in my transaction.rb file I set my default:
class Transaction < ActiveRecord::Base
def set_defaults
self.tran_date = Time.now.strftime("%d/%m/%Y")
end
end
When I run my scenario I get the following:
Scenario: Displaying defaults when entering a form # features/receipt_journal_new.feature:29
Given I am on the new transaction page
# features/step_definitions/web_steps.rb:45
And the time is Dec 31 2010, 9:00 UTC # features/step_definitions/timecop_steps.rb:1
Then the "Tran date" field should contain "31/12/2010" # features/step_definitions/web_steps.rb:142
expected: /31\/12\/2010/
got: "20/07/2011" (using =~)
Diff:
@@ -1,2 +1,2 @@
-/31\/12\/2010/
+20/07/2011
(RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/web_steps.rb:147:in `block (2 levels) in <top (required)>'
./features/step_definitions/web_steps.rb:30:in `with_scope'
./features/step_definitions/web_steps.rb:143:in `/^the "([^"]*)" field(?: within (.*))? should contain "([^"]*)"$/'
features/transaction_new.feature:32:in `Then the "Tran date" field should contain "31/12/2010"'
Update:
I've even tried stubbing out Time.now and that didn't work either:
Given 'the time is $time' do |time|
Time.stub!(:now).and_return("31/12/2010") #Time.parse(time))
# Timecop.freeze Time.parse(time)
end
Update2:
I've tried using chronic as well as using Timecop.travel instead of Timecop.freeze as in this article:
http://www.louismrose.me.uk/post/876230592/freezing-time-in-cucumber
Update3:
I've tested Timecop in my rspec model tests and it works fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案只是我在场景中设置 Timecop 的顺序
The answer was simply the order in which I was setting Timecop in my Scenario