所以我正在针对这个rspec运行我的方法,一切都过去了。但我无法帮助,但我认为时间安排应该抛弃它,是否有一些我不知道为什么不存在的东西? 即使该方法通过,rspec中的信息如何适用于该方法?
这是我的方法:
def measure(num=1, &block)
n=0
arr = []
while n <= num-1
a=Time.now
block.call
b=Time.now
arr << (b-a)
n += 1
end
sum = arr.inject(:+)
avgtime = sum/num
end
这是我的rspec文件的一部分:
@eleven_am = Time.parse("2011-1-2 11:00:00")
it "takes exactly 1 second to run a block that sleeps for 1 second (with stubs)" do
fake_time = @eleven_am
Time.stub(:now) { fake_time }
elapsed_time = measure do
fake_time += 60 # adds one minute to fake_time
end
elapsed_time.should == 60
end
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