0

以下是我在 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
4

1 回答 1

0

在您的规格中:

expect(station).to receive(release_x_broken_bikes: broken_bike)

你是在告诉电台期望收到一个release_x_broken_bikes参数为broken_bike? 看着方法,它想要一个数字。 broken_bike对我来说看起来像是双重的。

您的错误准确地告诉了您:

1) 货车可以装满损坏的自行车故障/错误: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' # ./spec/van_spec。 rb:51:in block (2 levels) in ' 在 0.00995 秒内完成 12 个示例,1 个失败

它接到了release_x_broken_bikeswith的电话10,这似乎是您真正想要的。告诉它期待这一点,你就可以解决下一个问题(如果有的话)。

于 2013-10-16T00:32:42.093 回答