raise an error on the xth time and after that return a value
<p>I want to use a rspec to simulate a flakey service handling.</p>

<p>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.</p>

<p>Is this possible with rspec?</p>

<p>I tried with</p>

<pre><code>allow(Service).to receive(:run).once.and_raise(MyError)
allow(Service).to receive(:run).once.and_return(response)
</code></pre>

<p>but on the first run it returns the response and not the error</p>