We recently started using MediatR to allow us to eliminate controller actions as we rearrange the large client portal and convert everything to C #. As part of this, we are also increasing the coverage of unit test, but I ran into a problem while trying to mock MediatR itself.
The team does a ton of things to initiate the process, and part of it sends a notification. The notification itself is processed by its own handler and therefore will obey its own unit test, so I want to make fun of MediatR so that calling this.mediator.Send(message) really does nothing. The handler returns the object, but we do not care about this in this context, therefore, for all purposes and purposes, we consider it as a void return. I just want to verify that Send was called once as part of the test. However, the Send method throws a NullReferenceException , and I don't know why.
Starting with version 3, MediatR now accepts the second optional parameter on Send , a CancellationToken , and expression trees require that you explicitly set them so that you specify a value. I have not encountered this before and, in my opinion, I feel that this may be part of the problem, but it may be a merger on my part.
Here is a snapshot.
SUT
public class TransferHandler : IAsyncRequestHandler<TransferCommand, TransferResult> { private readonly IMediator mediator; public TransferHandler(IMediator mediator) { this.mediator = mediator; } public async Task<TransferResult> Handle(TransferCommand message) {
Test
public class TransferHandlerTests { [Theory] [AutoData] public async void HandlerCreatesTransfer(Mock<IMediator> mockMediator) {
What am I missing? I feel like I made a fundamental mistake, but I donβt know where.
source share