我有一个结构如下的测试套件:
describe ... do
[list of dates].each do
describe
before(:all) do
base_date = ...
end
describe ... do
[list of times].each do
describe ... do
before(:all) do
base_time = base_date + ...
DateTime.stub!(:now).and_return(base_time)
end
describe ... do
<test using records within date-time range based on base_time>
end
describe ... do
<another test using records within date-time range based on base_time>
end
end
end
end
end
end
end
第一个测试有 DateTime(now) == base_time,但第二个测试为 DateTime(now) == 我的计算机的日期时间,表明存根不再有效。将stub!
调用移动到每个describe
循环中可以解决问题,但我想了解为什么它不能按书面方式工作。