[英]Mediatr: Unit Testing behaviors/validation
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. 我有一个将实体保存到数据库的命令/处理程序,但是在我的代码中,它首先使用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
我的验证器称为
CreateCoinCommandValidator
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 另请参阅默认测试扩展
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.