代码突然不再通过 [=10th=]...需要新的眼光
code suddenly dosen't pass rspec anymore...need fresh eyes
前几天我让这段代码通过了所有规范,所以我把它移到了另一个文件夹中(用于完成的作业)。当我尝试从那里再次 运行 规范时,尽管我(多次)准确地输入了我看到的路径,但我无法加载文件。嗯嗯。是否有一些特殊的方法可以从子文件夹中 运行 rspecs?不管怎样,所以我把它移回了它所在的主目录,并从那里尝试,突然它就无法通过了!!我一定是不小心删除了一些东西。但是对于我将近两天后的生活……我只是看不到它。我已经经历了一百万次,它似乎应该可以工作,当我从 class 中取出方法时,它们会按预期 return 。我需要新鲜的眼睛。为什么这段代码突然不通过了?
class :: Timer
attr_accessor :seconds, :padded, :time_string
def initialize(seconds = 0)
@seconds = seconds
end
def padded(num)
num = num.to_s
num.length == 1 ? "0#{num}" : "#{num}"
end
def time_string=(seconds)
@seconds = seconds
mins = 0
hours = 0
if seconds == 0
return "00:00:00"
elsif seconds < 60
return "00:00:#{padded(seconds)}"
end
while seconds > 59
mins += 1
seconds -= 60
if mins > 59
hours += 1
mins -= 60
end
end#while
return "#{padded(hours)}:#{padded(mins)}:#{padded(seconds)}"
end
end#class
规格如下:
require_relative '09_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
# One way to implement the Timer is with a helper method.
# Uncomment these specs if you want to test-drive that
# method, then call that method from inside of time_string.
#
describe 'padded' do
it 'pads zero' do
@timer.padded(0).should == '00'
end
it 'pads one' do
@timer.padded(1).should == '01'
end
it "doesn't pad a two-digit number" do
@timer.padded(12).should == '12'
end
end
end
class :: Timer
attr_accessor :seconds
def initialize(seconds = 0)
@seconds = seconds
end
def padded(num)
num = num.to_s
num.length == 1 ? "0#{num}" : "#{num}"
end
def time_string
seconds = @seconds
mins = 0
hours = 0
if seconds == 0
return "00:00:00"
elsif seconds < 60
return "00:00:#{padded(seconds)}"
end
while seconds > 59
mins += 1
seconds -= 60
if mins > 59
hours += 1
mins -= 60
end
end#while
return "#{padded(hours)}:#{padded(mins)}:#{padded(seconds)}"
end
end#class
规格
require_relative '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
describe 'time_string' do
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
# One way to implement the Timer is with a helper method.
# Uncomment these specs if you want to test-drive that
# method, then call that method from inside of time_string.
#
describe 'padded' do
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
end
end
我真的不知道你的代码是怎么搞砸的,但是 time_string
方法在你的规范中没有接受任何参数,我不明白为什么它会只是从逻辑的角度来看.因此我从它的方法签名中取出了 =(seconds)
部分。
您还将实例变量 @seconds
设置为传递的参数,但实际上我们想使用一个本地 seconds
变量,该变量开始时作为实例变量,这样就可以翻转了至 seconds = @seconds
。
最后,我更改了您的规范以使用新的 expect
语法,而不是 should
和 is now deprecated。
前几天我让这段代码通过了所有规范,所以我把它移到了另一个文件夹中(用于完成的作业)。当我尝试从那里再次 运行 规范时,尽管我(多次)准确地输入了我看到的路径,但我无法加载文件。嗯嗯。是否有一些特殊的方法可以从子文件夹中 运行 rspecs?不管怎样,所以我把它移回了它所在的主目录,并从那里尝试,突然它就无法通过了!!我一定是不小心删除了一些东西。但是对于我将近两天后的生活……我只是看不到它。我已经经历了一百万次,它似乎应该可以工作,当我从 class 中取出方法时,它们会按预期 return 。我需要新鲜的眼睛。为什么这段代码突然不通过了?
class :: Timer
attr_accessor :seconds, :padded, :time_string
def initialize(seconds = 0)
@seconds = seconds
end
def padded(num)
num = num.to_s
num.length == 1 ? "0#{num}" : "#{num}"
end
def time_string=(seconds)
@seconds = seconds
mins = 0
hours = 0
if seconds == 0
return "00:00:00"
elsif seconds < 60
return "00:00:#{padded(seconds)}"
end
while seconds > 59
mins += 1
seconds -= 60
if mins > 59
hours += 1
mins -= 60
end
end#while
return "#{padded(hours)}:#{padded(mins)}:#{padded(seconds)}"
end
end#class
规格如下:
require_relative '09_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
# One way to implement the Timer is with a helper method.
# Uncomment these specs if you want to test-drive that
# method, then call that method from inside of time_string.
#
describe 'padded' do
it 'pads zero' do
@timer.padded(0).should == '00'
end
it 'pads one' do
@timer.padded(1).should == '01'
end
it "doesn't pad a two-digit number" do
@timer.padded(12).should == '12'
end
end
end
class :: Timer
attr_accessor :seconds
def initialize(seconds = 0)
@seconds = seconds
end
def padded(num)
num = num.to_s
num.length == 1 ? "0#{num}" : "#{num}"
end
def time_string
seconds = @seconds
mins = 0
hours = 0
if seconds == 0
return "00:00:00"
elsif seconds < 60
return "00:00:#{padded(seconds)}"
end
while seconds > 59
mins += 1
seconds -= 60
if mins > 59
hours += 1
mins -= 60
end
end#while
return "#{padded(hours)}:#{padded(mins)}:#{padded(seconds)}"
end
end#class
规格
require_relative '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
describe 'time_string' do
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
# One way to implement the Timer is with a helper method.
# Uncomment these specs if you want to test-drive that
# method, then call that method from inside of time_string.
#
describe 'padded' do
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
end
end
我真的不知道你的代码是怎么搞砸的,但是 time_string
方法在你的规范中没有接受任何参数,我不明白为什么它会只是从逻辑的角度来看.因此我从它的方法签名中取出了 =(seconds)
部分。
您还将实例变量 @seconds
设置为传递的参数,但实际上我们想使用一个本地 seconds
变量,该变量开始时作为实例变量,这样就可以翻转了至 seconds = @seconds
。
最后,我更改了您的规范以使用新的 expect
语法,而不是 should
和 is now deprecated。