2011-07-20 51 views
0

我想一起使用黄瓜和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 

UPDATE2:

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

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

Update3:

我已经在我的rspec模型测试中测试了Timecop,它工作正常。

回答

0

的回答很干脆在我在我的情节设置时空特警顺序

Given the time is Dec 31 2010, 9:00 UTC 
And I am on the new transaction page