2014-09-28 47 views
0

我有一个页面可以创建文档的快照。例如,该文档以标题为时间戳(September 27, 2014 at 4:01:10 pm)保存。我正在为此页面撰写测试,并希望将时间存根以便不会更改。在水豚测试中存留时间

我目前所面对的是Time.stubs(:now).returns(Time.parse("2014-1-2 11:00:00"))但是当我做,我得到一个错误信息说:

水豚:: FrozenInTime:出现时间被冻结,水豚不与图书馆工作,其冻结时间,考虑用时间旅行代替

什么是最好的方法来停留在这里?

+2

你可能要检查出'timecop'宝石:https://github.com/travisjeffery/timecop – sjaime 2014-09-28 19:20:36

+0

@sjaime如果你将发布作为答案,我会选择它。这是超级有用的! – Zack 2014-09-28 20:42:55

回答

1

我在这里重新发布我的评论作为答案。

还有就是timecop宝石https://github.com/travisjeffery/timecop

它可以让你不喜欢的东西如下:

describe "some set of tests to mock" do 
    before do 
    Timecop.freeze(Time.local(1990)) 
    end 

    after do 
    Timecop.return 
    end 

it "should do blah blah blah" {} 
end 

这将使运行,如果它是1990年1月1日的测试,然后恢复到当前时间。现在includes support

0

Rails的直接时间旅行,如:

feature 'Time travel verifier' do 
    include ActiveSupport::Testing::TimeHelpers 

    scenario 'works in the past' do 
    travel_to(1.day.ago) do 
     visit time_travel_verification_path 
     expect(page).to have_content('WOAH Time Travel!') 
    end 
    end 
end