以下是我在 RSPEC 中的测试。它失败了,我不知道为什么!我正试图让它在货车上装满自行车,但将所有其他自行车留在车站。但是,它目前正在删除所有自行车,除了一辆。
let(:van) { Van.new }
let(:bike) { double :bike, broken?: false }
let(:broken_bike) { double :broken_bike, broken?: true }
let(:station) { double :station }
it 'can be filled with broken bikes' do
station = double :station, { release_x_broken_bikes: broken_bike }
expect(station).to receive(release_x_broken_bikes: broken_bike)
van.fill_with_broken_bikes_from station
expect(van.bike_count).to eq > 2
end
van.rb中的相关代码:
def initialize bikes = [], capacity = 10
@bikes = bikes
@capacity = capacity
end
def bike_count
@bikes.count
end
def fill_with_broken_bikes_from station
@bikes = station.release_x_broken_bikes(slots_available)
end
def slots_available
@capacity - bike_count
end
station.rb 中的代码:
def broken_bikes
@bikes.select { |bike| bike.broken? }
end
def release_broken_bike
@bikes.delete(broken_bikes.first)
end
def release_x_broken_bikes(x)
broken_bikes[0,x].map{ release_broken_bike}
end