Moq: Setup a mocked method to fail on the first call, succeed on the second

anthony picture anthony · Aug 12, 2011 · Viewed 14.1k times · Source

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?

Answer

rsbarro picture rsbarro · Aug 12, 2011

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();
}