title: How to throw an exception on the Nth call of a mock method with Mockery
body: <p>I need to test how some code I wrote will behave when it calls a method on another class multiple times, where one of the calls will cause an exception to be thrown.</p>
  
  <p>I am using <a href="http://docs.mockery.io" rel="noreferrer">Mockery</a> to mock the class that may throw an exception.</p>
  
  <p>So in my case, the method will be called three times and I need it throw an exception on the second time.</p>
  
  <p>This is example of my intention but it doesn't work.</p>
  
  <pre><code>$mock = \Mockery::mock();
  $mock-&gt;shouldReceive('fetch')
      -&gt;andReturnUsing(
          function () {return true;},
          function () use ($e) {throw new \Exception();},
          function () {return false;}
      );
  </code></pre>
  
  <p>I was given the impression the above might work from the response in <a href="https://github.com/mockery/mockery/issues/308#issuecomment-39601446" rel="noreferrer">Asserting that mock throws exception · Issue #308 · mockery/mockery</a>.</p>
  
  <p>However, in practice, throwing an exception this way causes Mockery to catch the exception and throw its own <code>BadMethodCall</code> exception.</p>