我有一個問題,只有當隨機數低於20時,這些測試纔會通過,我如何在測試中解釋這個問題?如何在我的rspec測試中考慮隨機數?
我的測試:
it 'a plane cannot take off when there is a storm brewing' do
airport = Airport.new [plane]
expect(lambda { airport.plane_take_off(plane) }).to raise_error(RuntimeError)
end
it 'a plane cannot land in the middle of a storm' do
airport = Airport.new []
expect(lambda { airport.plane_land(plane) }).to raise_error(RuntimeError)
end
我的代碼摘錄:
def weather_rand
rand(100)
end
def plane_land plane
raise "Too Stormy!" if weather_ran <= 20
permission_to_land plane
end
def permission_to_land plane
raise "Airport is full" if full?
@planes << plane
plane.land!
end
def plane_take_off plane
raise "Too Stormy!" if weather_ran <= 20
permission_to_take_off plane
end
def permission_to_take_off plane
plane_taking_off = @planes.delete_if {|taking_off| taking_off == plane }
plane.take_off!
end
或提供的隨機數發生器,它最終是更靈活反正的實現。 –