首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >黄瓜配timecop

黄瓜配timecop
EN

Stack Overflow用户
提问于 2011-07-20 15:11:12
回答 1查看 1.6K关注 0票数 0

我正在尝试同时使用cucumber和timecop,它的timecop似乎没有覆盖我的transaction.rb模型文件中的Time.now调用。

以下是我的场景:

代码语言:javascript
复制
  @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文件中包含了这些步骤

代码语言:javascript
复制
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文件中设置我的默认值:

代码语言:javascript
复制
class Transaction < ActiveRecord::Base
  def set_defaults
    self.tran_date = Time.now.strftime("%d/%m/%Y")
  end
end

当我运行我的场景时,我得到了以下结果:

代码语言:javascript
复制
  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,但也不起作用:

代码语言:javascript
复制
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:

我已经尝试使用chronic以及使用Timecop.travel代替Timecop.freeze,如本文所示:

http://www.louismrose.me.uk/post/876230592/freezing-time-in-cucumber

Update3:

我在我的rspec模型测试中测试了Timecop,它工作得很好。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-21 11:49:29

答案很简单,就是我在我的场景中设置Timecop的顺序

代码语言:javascript
复制
Given the time is Dec 31 2010, 9:00 UTC
And I am on the new transaction page
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6758241

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档