麻烦通过RSpec测试。时间不能被强制进入Fixnum

时间:2014-04-19 17:07:38

标签: ruby rspec

我在使用这个rspec测试时遇到了麻烦。我正在使用Ruby 1.93和RSPEC 2.14.8(我想)很抱歉这篇文章会有多长。我可以得到我发现的其他代码,但我不能让我的代码工作,我不知道为什么。

我的代码:

def measure(number = 1)  
  counter = number  
  total = 0  
  while counter > 0  
  start = Time.now  
  counter-=1  

yield

 stop = Time.now
 total = total + stop - start

end

 total / number
end  

我发现有效的其他代码:

def measure count=1  
     total_time = 0  
     count.times do  
     start_time = Time.now  

yield  

    end_time = Time.now  
    total_time += end_time - start_time  
end  
    total_time / count  
end  

测试:

require_relative "performance_monitor"  

require "time"  # loads up the Time.parse method -- do NOT create time.rb!  

describe "Performance Monitor" do  
  before do  
    @eleven_am = Time.parse("2011-1-2 11:00:00")  
  end  

  it "takes about 0 seconds to run an empty block" do  
    elapsed_time = measure do  
    end  
    elapsed_time.should be_within(0.1).of(0)  
  end  

  it "takes exactly 0 seconds to run an empty block (with stubs)" do  
    Time.stub(:now) { @eleven_am }  
    elapsed_time = measure do  
    end  
    elapsed_time.should == 0  
  end  

  it "takes about 1 second to run a block that sleeps for 1 second" do  
    elapsed_time = measure do  
      sleep 1  
    end  
    elapsed_time.should be_within(0.1).of(1)  
  end  

  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 "runs a block N times" do  
    n = 0  
     measure(4) do  
         n += 1  
    end  
    n.should == 4  
   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  

  it "returns the average time when running a random number of times for random lengths of time" do   
    fake_time = @eleven_am  
    Time.stub(:now) { fake_time }  
    number_of_times = rand(10) + 2  
    average_time = measure(number_of_times) do  
      delay = rand(10)  
      fake_time += delay  
    end  
    average_time.should == (fake_time - @eleven_am).to_f/number_of_times  
  end  

end  

我跑的时候说的是什么:

  

性能监视器大约需要0秒才能运行空块   (失败 - 1)

     

故障:

     

1)性能监视器需要大约0秒来运行空块
       失败/错误:elapsed_time =措施确定        类型错误:
         时间不能强制进入Fixnum        #./06_performance_monitor/performance_monitor.rb:11:in'+'#。/ 06_performance_monitor / performance_monitor.rb:11:'measure'          #。/ 06_performance_monitor / performance_monitor_spec.rb:21:在'块(2级)中'

     

以0.008秒完成1例,1次失败

1 个答案:

答案 0 :(得分:2)

total = total + stop - starttotal = total + (stop - start)

不同

第一个实际上是评估为total = (total + t) - n,因为您无法一起添加两次,所以它不起作用。

我认为你的意思是:

total = total + (stop - start)或更简洁total += stop - start