How to use Callback on Moq’s SetUpSequence?

I’m using Moq version 4.8 and have a method to mock and assert its parameter. I started with this mocked method:

mock.Setup(m => m.Update(It.IsAny<MyClass>))
    .Callback((MyClass c) =>
    {
        // some assertions
    })
    .Returns(Task.FromResult(updatedClass));

where I update an object of type MyClass and do a number of assertions on that object. This works just fine.

I’ve just added logic to the method calling Update to retry calling it if exceptions are thrown. So I want to implement a new unit test that throws exceptions a few times and then returns and be able to do the assertions like before. So I tried SetupSequence as follows:

mock.SetupSequence(m => m.Update(It.IsAny<MyClass>))
    .Throws(new Exception("test exception 1"))
    .Throws(new Exception("test exception 2"))
    .Callback((MyClass c) =>
    {
        // some assertions
    })
    .Returns(Task.FromResult(updatedClass));

But ISetupSequence doesn’t support Callback. Is there a way to mock Throws and Returns calls in order while keeping a pre call Callback to Returns?

Answer

For the time being I’ve been doing the this as a work around:

int callCount = 0;
mock.Setup(m => m.Update(It.IsAny<MyClass>))
    .Callback((MyClass c) =>
    {
        callCount++;
        if (callCount <=2)
        {
            throw new Exception($"Test Exception #{callCount}");
        }
        else
        {
            callCount = 0; // this is needed if you're gonna use callCount for multiple setups
            // some assertions
        }
    })
    .Returns(Task.FromResult(updatedClass));

Feels like a hack, but does what I’m looking for.

Leave a Reply

Your email address will not be published. Required fields are marked *