据我了解,每次它通过 run_times 运行时,它都会弹出一个 run_times 值并将其添加到 fake_time。使用我的测量方法,我应该收集这些并得到平均值。但我似乎无法从方法中获取值。
它需要通过以下rspec:
it "returns the average time, not the total time, when running multiple times" do
run_times = [8,6,5,7]
fake_time = @eleven_am
Time.stub(:now) { fake_time }
average_time = measure(4) do
fake_time += run_times.pop
end
average_time.should == 6.5
end
这是我的代码:
def measure (n = 1)
if n > 1
n.times { yield }
else
start = Time.now
yield
elapsed_time = Time.now - start
elapsed_time
end
end
先感谢您!我已经坚持了一段时间,似乎无法得到任何工作。