簡體   English   中英

RSpec圍繞time_shared_example

[英]Rspec surrounding shared_example with timecop

我正在嘗試將Timecop包裝在我編寫的shared_example規范周圍

describe "timecop wrapping shared example" do
   Timecop.freeze(1.day.ago) do
      it_behaves_like "a shared example i would like to test using timecop"
   end
end

shared_examples "a shared example i would like to test using timecop"
   before :all do
      puts "the current time is #{Time.now}"
   end

   ... expect ...
end

但是運行此規范仍然使用實時時間,而不是凍結的時間

Timecop可以這樣工作嗎?

我還能如何包裝測試文件的大塊但更改其運行時間

Timecop需要在it內或before塊中執行。

以下更改將解決您的問題。

describe "timecop wrapping shared example" do before { Timecop.freeze(1.day.ago) } it_behaves_like "a shared example i would like to test using timecop" end

運行每個示例后,Rspec將重置環境(包括Timecop)。

我在測試中發現的一些時間旅行提示可能很有用:

  • Timecop.travel更能反映您的代碼在現實生活中的工作方式,因為時間永遠不會停滯不前。 您可以像凍結一樣使用它,只需換出凍結方法即可旅行。

  • 如果您使用Rails 4.1或更高版本,則內置了很棒的時間旅行工具,您可以放下timecop寶石。 如果您想了解更多,這是一篇博客文章文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM