What's the most succinct way to use Moq to mock a method that will throw an exception the first time it is called, then succeed the second time it is called?
I would make use of Callback
and increment a counter to determine whether or not to throw an exception from Callback
.
[Test]
public void TestMe()
{
var count = 0;
var mock = new Mock<IMyClass>();
mock.Setup(a => a.MyMethod()).Callback(() =>
{
count++;
if(count == 1)
throw new ApplicationException();
});
Assert.Throws(typeof(ApplicationException), () => mock.Object.MyMethod());
Assert.DoesNotThrow(() => mock.Object.MyMethod());
}
public interface IMyClass
{
void MyMethod();
}