环路
问题描述:
使用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
我该怎么做?
答
只需存根一次,并提供all the return values。
expect(Thing).to receive(:status)
.with(for: 'something')
.and_return('down', 'up')
第一次status
被调用时,它会返回'down'
。第二次将返回'up'
。