ruby - Issue with Rspec Timer test -
i stuck in second gear, please review code , give me input. thank you.
class timer def initialize(seconds = 0,time_string = "00:00:00") @seconds = seconds @time_string = time_string end def seconds=(new_sec) @seconds = new_sec end def seconds @seconds end def time_string=(new_time) hh = seconds/3600 mm = seconds%3600/60 ss = seconds%60 new_time = "#{hh}:#{mm}:#{ss}" @time_string = new_time end def time_string @time_string end end
rspec:
require 'timer' describe "timer" before(:each) @timer = timer.new end "should initialize 0 seconds" @timer.seconds.should == 0 end describe 'time_string' "should display 0 seconds 00:00:00" @timer.seconds = 0 @timer.time_string.should == "00:00:00" end "should display 12 seconds 00:00:12" @timer.seconds = 12 @timer.time_string.should == "00:00:12" end "should display 66 seconds 00:01:06" @timer.seconds = 66 @timer.time_string.should == "00:01:06" end "should display 4000 seconds 01:06:40" @timer.seconds = 4000 @timer.time_string.should == "01:06:40" end end
the following assignment def time_string=(new_time)
you're not using new_time change value of anything, better def time_string
define getter only. (your tests don't indicate want ability set time via supplied time_string.)
and neil slater points out don't need have instance variable @time_string, return new_time time_string method , have want. code...
def time_string=(new_time) hh = seconds/3600 mm = seconds%3600/60 ss = seconds%60 new_time = "#{hh}:#{mm}:#{ss}" @time_string = new_time end
replace with
def time_string hh = @seconds/3600 mm = @seconds%3600/60 ss = @seconds%60 new_time = "#{hh}:#{mm}:#{ss}" end
Comments
Post a Comment