Mock a return type of Task and Tuple with Moq

moq logo

Issue

Using Moq I was creating a few unit tests and stumbled across an interesting problem. I needed to mock a call to an async method on a service under test that took in a 2 parameters and returned a tuple of 2 values.

     (dbSomething, error) = await _myService.Create(something, null);

Solution

After a bit of battling I used Moq’s “ReturnAsync”

        _myMockedService.Setup(t => t.Create(It.IsAny<something>(), null))
            .ReturnsAsync((object something, string error) =>
            {
                return (null, "some expected errror");
            });

I’ve removed any business context

Leave a Reply