以下是我在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
答案 0 :(得分:0)
在您的规范中:
expect(station).to receive(release_x_broken_bikes: broken_bike)
您告诉电台预计会收到release_x_broken_bikes
,其参数为broken_bike
?看看这个方法,它需要一个数字。 broken_bike
对我来说似乎是双重的。
您的错误正好告诉您:
1)Van可以用破碎的自行车填充失败/错误:van.fill_with_broken_bikes_from station Double:station收到意外消息:release_x_broken_bikes with(10)#。/ lib / van.rb:29:in fill_with_broken_bikes_from'#。/ specl /van_spec.rb:51:in块(2级)'完成0.00995秒12个例子,1个失败
它收到release_x_broken_bikes
10
的电话,这似乎是你真正想要的。告诉它期待,你可以得到你的下一个问题(如果有的话)。