我试图将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能这样工作吗?
否则,我如何包装我的测试文件的大片段,但改变它运行的时间。
发布于 2017-08-19 00:13:00
Timecop需要在it块内或之前块中执行。
以下更改将解决您的问题。
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)。
我在测试中发现了一些在时间旅行中可能有用的技巧:
https://stackoverflow.com/questions/45760594
复制相似问题