環路

2017-08-16 32 views
1

使用RSpec的RSpec的存根同樣的方法,我想:環路

expect(Thing).to receive(:status) 
       .with(for: 'something') 
       .and_return('down') 

在第一次循環,和相同的存根應返回2號迭代不同的回報:

expect(Thing).to receive(:status) 
       .with(for: 'something') 
       .and_return('up') 

時測試以下代碼片段:

2.times do |i| 
    break if Thing.status(for: 'something') == 'up' 
    sleep 2 
    raise MyError if i > 0 
end 

我該怎麼做?

回答

5

只需存根一次,並提供all the return values

expect(Thing).to receive(:status) 
       .with(for: 'something') 
       .and_return('down', 'up') 

第一次status被調用時,它會返回'down'。第二次將返回'up'