尝试使用一个类(自行车)中的方法在我的第二类(DockingStation)中实现
class DockingStation
attr_reader :bikes, :capacity
attr_accessor :capacity
DEFAULT_CAPACITY = 20
def initialize(capacity = DEFAULT_CAPACITY)
@bikes = []
@capacity = capacity
end
def release_bike
fail 'No bikes available' if empty?
@bikes
end
def dock(bike)
fail 'bike full' if full?
@bikes << bike
end
def full?
@bikes.count >= DEFAULT_CAPACITY
end
def empty?
@bikes.empty?
end
private :full? , :empty?
end
class Bike
def working?
true
end
def report_broken
@broken = true
end
def broken?
@broken
end
end
require './lib/docking_station.rb'
require './lib/bike.rb'
describe DockingStation do
describe 'initialization' do
subject{DockingStation.new}
let(:bike) {Bike.new}
it 'default capacity' do
described_class::DEFAULT_CAPACITY.times do
subject.dock(bike)
end
expect{ subject.dock(bike) }.to raise_error 'bike full'
end
end
it 'has a default capacity' do
expect(subject.capacity).to eq DockingStation::DEFAULT_CAPACITY
end
describe '#release_bike' do
it 'releases a bike' do
bike = Bike.new
我希望我的RSPEC测试通过(下方),但我一直遇到任何没有提出的错误。我的目标是让我的车站不释放停靠时报告的破损自行车,但是方法破了?是否在我的自行车文件中,所以我无法用我的方法写“失败的“自行车损坏”,如果损坏了?由于对接站文件无法识别该方法。在正确方向上的任何帮助都非常感谢
it 'raises an error if bike is broken' do
bike = Bike.new
bike.report_broken
subject.dock bike
expect {subject.release_bike}.to raise_error 'No bikes available'
end
class DockingStation
attr_reader :bikes, :capacity
attr_accessor :capacity
DEFAULT_CAPACITY = 20
def initialize(capacity = DEFAULT_CAPACITY)
@bikes = []
@capacity = capacity
end
def release_bike
fail 'No bikes available' if empty?
@bikes
end
def dock(bike)
fail 'bike full' if full?
@bikes << bike
end
def full?
@bikes.count >= DEFAULT_CAPACITY
end
def empty?
@bikes.empty?
end
private :full? , :empty?
end
class Bike
def working?
true
end
def report_broken
@broken = true
end
def broken?
@broken
end
end
require './lib/docking_station.rb'
require './lib/bike.rb'
describe DockingStation do
describe 'initialization' do
subject{DockingStation.new}
let(:bike) {Bike.new}
it 'default capacity' do
described_class::DEFAULT_CAPACITY.times do
subject.dock(bike)
end
expect{ subject.dock(bike) }.to raise_error 'bike full'
end
end
it 'has a default capacity' do
expect(subject.capacity).to eq DockingStation::DEFAULT_CAPACITY
end
describe '#release_bike' do
it 'releases a bike' do
bike = Bike.new
I want my rspec test to pass( down below) but I keep getting the error that nothing was raised. My goal is for my station to not release a broken bike that was reported when docked but the method broken? is in my bike file and so I can't in my method write 'fail 'bike broken' if broken? as the docking station file doesn't recognise the method. Any help in the right direction is greatly appreciated
it 'raises an error if bike is broken' do
bike = Bike.new
bike.report_broken
subject.dock bike
expect {subject.release_bike}.to raise_error 'No bikes available'
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果停靠站没有自行车或仅损坏的自行车,您的测试看起来会引起错误。
但是您的
Release_bike
当前没有考虑到损坏的自行车:我想如果将该方法更改为以下方法,则您的测试应通过:
哪个首先从
@bikes 不损坏的数组,然后仅检查是否有不间断的自行车。
Your test looks like it should raise an error if there are no bikes in a docking station or only broken bikes.
But your
release_bike
method currently doesn't take broken bikes into account:I guess your test should pass if you change that method to the following:
Which first selects all bikes from the
@bikes
array that are not broken and then checks only for if there are non-broken bikes available.