How to instantiate Mediatr as part of a Unit Test?

You’re on the right lines with or a mockup – you need to mock the IMediator

There’s a few mocking libraries out there:

  • Moq
  • FakeItEasy
  • NSubstitute

Moq is one of the most popular, so, using your test as an example:

[Fact]
public async void UpdateCustomerCommand_CustomerDataUpdatedOnDatabase()
{
    //Arrange
    var mediator = new  Mock<IMediator>();

    UpdateCustomerCommand command = new UpdateCustomerCommand();
    UpdateCustomerCommandHandler handler = new UpdateCustomerCommandHandler(mediator.Object);

    //Act
    Unit x = await handler.Handle(command, new System.Threading.CancellationToken());

    //Assert
    //Do the assertion

    //something like:
    mediator.Verify(x=>x.Publish(It.IsAny<CustomersChanged>()));
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)