我是一个 Ruby 新手,试图用以下 Rspec 创建一个 Timer 类:
require 'timer'
describe "Timer" do
before(:each) do
@timer = Timer.new
end
it "should initialize to 0 seconds" do
@timer.seconds.should == 0
end
describe 'time_string' do
it "should display 0 seconds as 00:00:00" do
@timer.seconds = 0
@timer.time_string.should == "00:00:00"
end
it "should display 12 seconds as 00:00:12" do
@timer.seconds = 12
@timer.time_string.should == "00:00:12"
end
it "should display 66 seconds as 00:01:06" do
@timer.seconds = 66
@timer.time_string.should == "00:01:06"
end
it "should display 4000 seconds as 01:06:40" do
@timer.seconds = 4000
@timer.time_string.should == "01:06:40"
end
end
但我不明白 Rspec 的返回错误消息,上面写着“计时器应该初始化为 0 秒”,我一开始就卡在我的代码上,非常感谢任何能解释下面我的代码有什么问题的人。谢谢。
class Timer
def intialize(seconds)
@seconds = seconds
end
def seconds=(new_seconds = 0)
@seconds = new_seconds
end
def seconds
@seconds
end
end