6

I have a command/handler that saves an entity to the db, but in my code, it goes through validation first (validation pipeline) using fluentvalidation.

I was able to create a success test to test the handler, but now I would like to make sure the command goes through validation first.

How would I go about doing so? should I be calling the validation independently like i do with my handler? if so how do i do that

here is my code

    [Test]
    public  async Task CreateCoinCommand_Success()
    {
        var context = new Mock<EventsContext>();
        var ownersMock = CreateDbSetMock(new List<Owner>());

        context.Setup(x => x.Owners).Returns(ownersMock.Object);

        var handler = new CreateCoinCommandHandler(context.Object, mapper.Object );


        var cmd = new CreateCoinCommand(1, "sym", "name", null, null, null, 1, "description",
            null, "https://google.com", null, null, null, new []{1,2});

        var cltToken = new System.Threading.CancellationToken();
        var result = await handler.Handle(cmd, cltToken);

        Assert.IsInstanceOf<Unit>(result);
    }

My validator is called CreateCoinCommandValidator

Zoinky
  • 4,083
  • 11
  • 40
  • 78

1 Answers1

5

Yes, in unit test you need to call validator manually

// Arrange
var validator = new CreateCoinCommandValidator();
var cmd = new CreateCoinCommand(1, "sym", "name", null, null, null, 1, "description",
            null, "https://google.com", null, null, null, new []{1,2});

// Act
var validationResult = await validator.ValidateAsync(cmd);

// Assert
Assert.True(validationResult.IsValid);
...

Also see Default testing extensions

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116