[英]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)。
我在測試中發現的一些時間旅行提示可能很有用:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.