2016-12-22 25 views
0

我知道如何通過在time_string方法內部實現複雜的條件語句網絡來解決此問題。但是,我試圖使用Symbol#to_proc方法的新知識。正如你所看到的,我建立了'填充'方法,我試圖用'&'語法來調用它,但是我不斷收到此錯誤消息。我嘗試了許多不同的格式,但無濟於事。測試第一個Ruby - 定時器:在time_string方法中實現Symbol#to_proc的問題

我不斷收到此相同的錯誤消息:

# # 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 

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的代碼:

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

我的定時器類從timer.rb定義來自我的終端的錯誤消息:

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 
+0

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

至於反對時間)「和」def秒「。當你編寫attr_accessor時:秒ruby會爲你做這個 – sig

+0

修正你的spec的縮進,並且錯誤變得明顯。 – Stefan

+0

剛剛編輯規範,將「結束」移到底部,在描述的「定時器」範圍內包含describe'time_string'。現在我得到這個....失敗/錯誤:期望(@ timer.time_string).to eq(「00:00:00」) NoMethodError: 未定義的方法'填充'爲0:Fixnum – Sedulity

回答

0

你有幾個「描述」塊。您只在第一次定義「@timer」。所以,你應該複製

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

到每個的 「描述」 S。或者將這個塊移到更高的範圍。

還不錯的做法是使用rspec`s let and let!

0

需要以下的解決辦法:在你的情況你不需要添加方法「高清秒=(

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

是因爲'padded'方法對於這個類是本地的,不包含在Ruby庫中? @Stefan – Sedulity

+0

https://andrewjgrimm.wordpress.com/2011/10/03/in-ruby-method-passes-you/ – Sedulity

相關問題