我正在 TestFirst Ruby 中开始 06_performance_monitor 练习,它列出的失败出现在 TestFirst Ruby 教程材料中提供的 Rspec 中(其中显示未定义的方法“测量”)。从我在网上找到的内容来看,本教程似乎是为 Rspec 2 和 Ruby 1.9 编写的。我目前安装了 Rspec 2.99.0、Ruby 1.9.3 和 Rake 0.9.22。我想我的问题是,我做错了什么/我需要不同的版本吗?我不明白为什么提供的 Rspec 似乎无法正常工作。这发生在本教程的另一个练习中,但这只是一个小错误消息,不会干扰我的测试。
caitlyns-mbp:06_performance_monitor caitlynyu$ rake
(in /Users/caitlynyu/Desktop/learn_ruby)
Run options: include {:focus=>true}
All examples were filtered out; ignoring {:focus=>true}
Performance Monitor
runs a block N times (FAILED - 1)
Failures:
1) Performance Monitor runs a block N times
Failure/Error: measure(4) do
NoMethodError:
undefined method `measure' for #<RSpec::Core::ExampleGroup::Nested_1:0x007fcdc11b8578>
# ./06_performance_monitor/performance_monitor_spec.rb:53:in `block (2 levels) in <top (required)>'
Finished in 0.00081 seconds
1 example, 1 failure
Failed examples:
rspec ./06_performance_monitor/performance_monitor_spec.rb:51 # Performance Monitor runs a block N times
Randomized with seed 2933
/Users/caitlynyu/.rvm/rubies/ruby-1.9.3-p547/bin/ruby -S rspec /Users/caitlynyu/Desktop/learn_ruby/06_performance_monitor/performance_monitor_spec.rb -I/Users/caitlynyu/Desktop/learn_ruby/06_performance_monitor -I/Users/caitlynyu/Desktop/learn_ruby/06_performance_monitor/solution -f documentation -r ./rspec_config failed
caitlyns-mbp:06_performance_monitor caitlynyu$
更新
我的代码如下(我知道它没有通过下面的任何测试,我只是先玩这个):
def performance_monitor
start = Time.now
yield
return "#{Time.now - start}"
end
我从未使用过出现在 Rspec 中的名为“measure”的方法,如下所示:
# # Topics
#
# * stubs
# * blocks
# * yield
#
# # Performance Monitor
#
# This is (a stripped down version of) an actual useful concept: a
# function that runs a block of code and then tells you how long it
# took to run.
require "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