2017-03-18 7 views
0

通常、1から10までの乱数を返すときにメソッドが特定の値を返すと仮定するrspecテストを作成しようとしているここでメソッドがRspecで特定の値を返すようにする(そうでなければランダムになる)

は私のlibディレクトリ内のコードです:

def take_off(plane) 
    fail "Bad weather today. Cannot take off." if stormy_weather? > 8 
    plane.take_off_plane 
    planes.delete(plane) 
    end 

    def stormy_weather? 
    rand(10) 
    end 

そして、ここでは、RSpecの中に私のテストで:

it 'raises an error when weather is stormy' do 
    plane = double(:plane) 
    stormy_weather = 9 
    allow(plane).to receive(:take_off_plane) 
    expect{ subject.take_off(plane) }.to raise_error("Bad weather today. Cannot take off.") 
end 

ができます誰にも事前にありがとうございます!

答えて

1

あなたはand_returnで返された値を指定することができます。

allow(subject).to receive(:stormy_weather?).and_return(9) 
+0

はいああ!私は実際にそれを 'allow(subject).to receive(:stormy_weather?)にしなければなりませんでした。and_return(9)' – Freddy

+1

@Freddy –

関連する問題