在第x次提出错误并在此之后返回一个值(raise an error on the xth time and after that return a value)
我想使用rspec来模拟Flakey服务处理。
为此,我想让服务调用引发异常几次,并在这些时间之后返回实际值。
这可能与rspec?
我尝试过
allow(Service).to receive(:run).once.and_raise(MyError) allow(Service).to receive(:run).once.and_return(response)但在第一次运行时它返回响应而不是错误
I want to use a rspec to simulate a flakey service handling.
For that, I want to make the service call raise an exception for a few times and after those times to return the real value.
Is this possible with rspec?
I tried with
allow(Service).to receive(:run).once.and_raise(MyError) allow(Service).to receive(:run).once.and_return(response)but on the first run it returns the response and not the error
最满意答案
您可以使用响应的块实现完成此操作。
call_count = 0 allow(Service).to receive(:run) do call_count += 1 call_count < 3 ? raise(MyError) : response endYou can accomplish this with a block implementation for the response.
call_count = 0 allow(Service).to receive(:run) do call_count += 1 call_count < 3 ? raise(MyError) : response end更多推荐
发布评论