Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 什么是假时间+;=rspec平均值为60?_Ruby_Rspec - Fatal编程技术网

Ruby 什么是假时间+;=rspec平均值为60?

Ruby 什么是假时间+;=rspec平均值为60?,ruby,rspec,Ruby,Rspec,我正在学习rspec关于使用“时间”的教程。有人能解释一下下面的代码是什么意思吗 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 t

我正在学习rspec关于使用“时间”的教程。有人能解释一下下面的代码是什么意思吗

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
我知道它应该需要60秒,但从技术上讲,我只是将60秒添加到时间变量
false\u time
,它应该是瞬时的。为什么需要60秒

下面是我编写的
measure
函数的代码。假设它测量运行一段代码所需的时间

def measure
  m1 = Time.now
  num.times { yield }
  m2 = Time.now
  m2 - m1
end

这个测试使用的是basic。它不会使方法花费额外的时间-它只是修改
time.now
返回的“stubbed”值。这对于测试非常有用,以确保在一定时间“已经过去”后,将返回预期值。

感谢您的响应,这非常有用。