0

I am playing around with the Clean Architecture by Ardalis.

Can someone please explain what this NoOpMediator role in Unit testing?

using System.Runtime.CompilerServices;
using MediatR;

namespace Clean.Architecture.UnitTests;

public class NoOpMediator : IMediator
{
  public Task Publish(object notification, CancellationToken cancellationToken = default)
  {
    return Task.CompletedTask;
  }

  ...
  ...
}

It does not seem to do anything. For four of its methods it returns Task.CompletedTask. And for the rest of the two it returns Task.FromResult<object?>(default)

Further more in the BaseEfRepoTestFixture class here, there is no configuration of Mock object. Bit misty.

Also noted that this is configured in CustomWebApplicationFactory way down here.

services.AddScoped<IMediator, NoOpMediator>();

Even if I comment out that line, the tests still pass.

Units tests sill passing even after NoOpMediator Configuration is removed

There should be an example test to show how this works.

Jesse
  • 3,243
  • 1
  • 22
  • 29
VivekDev
  • 20,868
  • 27
  • 132
  • 202
  • 1
    Just a guess: As `NoOpMediator` returns `IMediator` you could use https://github.com/ardalis/CleanArchitecture/blob/472f0f8e213b2bd466da025c5fea05be1a13f5b8/tests/Clean.Architecture.IntegrationTests/Data/BaseEfRepoTestFixture.cs for mocking. At least there is shown how it's mocked: `var mockMediator = new Mock();` – David Mar 19 '22 at 05:52
  • 1
    I'm not convinced that the sense of `NoOpMediator` is this, but perhaps it helps somehow: https://en.wikipedia.org/wiki/Null_object_pattern . Perhaps you should ask in the repository about the sense of the class. – David Mar 19 '22 at 06:18
  • 1
    I went to [this clean arch repo issues](https://github.com/ardalis/CleanArchitecture/issues/new/choose) and clicked question. And that got me here. Any way I will try to create a test with mockMediator as you suggested. – VivekDev Mar 19 '22 at 08:17

0 Answers0