【问题标题】:Test First Ruby - Timer : problems implementing Symbol#to_proc within the time_string methodTest First Ruby - Timer:在 time_string 方法中实现 Symbol#to_proc 的问题
【发布时间】:2017-05-08 08:37:57
【问题描述】:

我知道如何通过在 time_string 方法中实现复杂的条件语句网络来解决这个问题。但是,我正在尝试使用我对 Symbol#to_proc 方法的新知识。如您所见,我构建了“填充”方法,并尝试使用“&”语法调用它,但我不断收到此错误消息。我尝试了许多不同的格式,但都无济于事。

我不断收到同样的错误消息:

"Undefined method 'seconds=' for nil:NilClass"

来自 timer.rb 的我的 Timer 类定义:

class Timer
  attr_accessor :seconds

  def initialize
    @seconds = 0
  end

  def padded n
    "#{n}".rjust(2, '0')
  end

  def time_string
    t_hours = @seconds / 3600
    t_mins = (@seconds % 3600) / 60
    t_seconds = @seconds % 60
    @time_string = [t_hours, t_mins, t_seconds].map(&:padded).join(":")
  end
end

来自 timer_spec.rb 的 Rspec 代码:

# # Topics
#
# * classes
# * instance variables
# * string formats
# * modular arithmetic
#
# # Timer

require '09_timer'

describe "Timer" do
  before(:each) do
    @timer = Timer.new
  end

  it "should initialize to 0 seconds" do
      expect(@timer.seconds).to eq(0)
  end


  it "pads zero" do
    expect(@timer.padded(0)).to eq("00")
  end
  it "pads one" do
    expect(@timer.padded(1)).to eq("01")
  end
  it "doesn't pad a two-digit number" do
    expect(@timer.padded(12)).to eq("12")
  end

  describe "time_string" do

    before(:each) do
      @timer = Timer.new
    end

    it "should display 0 seconds as 00:00:00" do
      @timer.seconds = 0
      expect(@timer.time_string).to eq("00:00:00")
    end

    it "should display 12 seconds as 00:00:12" do
      @timer.seconds = 12
      expect(@timer.time_string).to eq("00:00:12")
    end

    it "should display 66 seconds as 00:01:06" do
      @timer.seconds = 66
      expect(@timer.time_string).to eq("00:01:06")
    end

    it "should display 4000 seconds as 01:06:40" do
      @timer.seconds = 4000
      expect(@timer.time_string).to eq("01:06:40")
    end
  end
end

来自我的终端的错误消息:

MacBook-Air:test1 ***$ bundle exec rspec spec/09_timer_spec.rb

Timer
  should initialize to 0 seconds
  pads zero
  pads one
  doesn't pad a two-digit number
  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: expect(@timer.time_string).to eq("00:00:00")
     NoMethodError:
       undefined method `padded' for 0:Fixnum

【问题讨论】:

  • 在您的情况下,您不需要手动添加方法“def seconds=(time)”和“def seconds”。当您编写 attr_accessor :seconds 时,ruby 会为您完成
  • 修正你的规范的缩进,错误就很明显了。
  • 刚刚编辑了规范,将“结束”移动到底部以在描述“计时器”范围内包含描述“时间字符串”。现在我得到这个 ....Failure/Error: expect(@timer.time_string).to eq("00:00:00") NoMethodError: undefined method `padded' for 0:Fixnum

标签: ruby class methods rspec instance-variables


【解决方案1】:

需要以下修复:

@time_string = [t_hours, t_mins, t_seconds].map(&method(:padded)).join(":")

相对于:

@time_string = [t_hours, t_mins, t_seconds].map(&:padded).join(":")

【讨论】:

【解决方案2】:

您几乎没有“描述”块。您只在第一个定义了“@timer”。所以你应该复制

before(:each) do
  @timer = Timer.new
end

进入每个“描述”。或者将此块移动到更高的范围。

同样好的做法是使用rspec`s let and let!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多