1

我收到错误:

Timer
  should initialize to 0 seconds
  time_string
      should display 0 seconds as 00:00:00 (FAILED - 1)

 Failures:

  1) Timer time_string should display 0 seconds as 00:00:00
     Failure/Error: @timer.seconds = 0
     NoMethodError:
       undefined method `seconds=' for #<Timer:0x007fd68b27c8d8>

运行以下规范

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

我的代码:

class Timer

  def timer=(timer  )
    @timer = timer
  end

  def seconds (x = 0)
    @timer = x
  end

  def time_string
   Time.at(@timer).utc.strftime("%H:%M:%S")
  end
 end

我得到了第一个初始化为 0 的测试,但我认为我做的不对。

此外,当我在终端 ruby​​ 的 time_string 方法中运行代码时,它会返回正确的时间。但我只是不知道如何在 @timer.seconds = 0 或 ...seconds = 12 等时将其发回。

提前致谢!

4

0 回答 0