Our unit tests have a lot of repetitive setups in them because we're using the MediatR library. We frequently have lines that look like this:
mockMediator.Setup(m => m.Send(It.IsAny<Command>(), It.IsAny<CancellationToken>())
.ReturnsAsync("ok");
I would like to make an extension method that simplifies the syntax to put more emphasis on Command, and still lets us be conditional if we want to, like:
mockMediator.SetupSend(It.IsAny<Command>())
.ReturnsAsync("ok");
// or
mockMediator.SetupSend(It.Is<Command>(c => c.IsSomething))
.ReturnsAsync("ok");
This code builds, however it does not work (the mock always returns null):
public static ISetup<IMediator, Task<TResult>> SetupRequest<TResult>(this Mock<IMediator> mockMediator, IRequest<TResult> request) {
return mockMediator.Setup(m => m.Send(request, It.IsAny<CancellationToken>()));
}
Fundamentally: how can I pass a parameter into a method that goes into an expression body later? Is there a supported way to make Moq "helper" functions like this for commonly-used setups?