简体   繁体   English

如何对返回无效值的命令处理程序进行单元测试?

[英]How do I unit test a Command Handler that returns a void?

Please see the code below: 请参见下面的代码:

public class CreatePersonHandler
        : IRequestHandler<CreatePersonCommand,Unit>
    {

 public async Task<Unit> Handle(CreatePersonCommand message, CancellationToken cancellationToken)
        {
            var person = _enquiryFactory.Create(message.Gender, message.Salary);
            var offers = getAvailableOffers(); 
            person.AssignOffers(offers);
            await _mediator.DispatchDomainEventsAsync(person);
            return Unit.Value;
        }
  }

Notice that: 注意:

1) The command does not have any state. 1)该命令没有任何状态。
2) The command method has no return value. 2)命令方法没有返回值。

I have read a few similar questions on here eg this one: Unit testing void methods? 我在这里已经阅读了一些类似的问题,例如:一个单元测试无效方法? . Are CQRS command handlers that return voids classed as informational and should not be unit tested? 返回空值的CQRS命令处理程序是否被归类为信息性的,不应进行单元测试?

Fundamentally, testing a "command handler" that returns a void is no different from testing a method that returns a void -- the void return is a clear indication that the method is invoked for its side effects, so you check for those. 从根本上说,测试返回一个void的“命令处理程序”与测试返回一个void的方法没有什么不同-void返回清楚地表明该方法因其副作用而被调用,因此您要检查这些副作用。

Depending on the nature of the subject under test, it may make sense to use a test double , rather than a live collaborator, to determine whether or not the right thing happened. 根据被测对象的性质,使用测试double而不是实时协作者来确定是否发生了正确的事情可能是有意义的。

var person = _enquiryFactory.Create(message.Gender, message.Salary);
var offers = getAvailableOffers(); 
person.AssignOffers(offers);
await _mediator.DispatchDomainEventsAsync(person);
return Unit.Value;

One thing to notice in this example is that, not only is there no state in your handler, but there is also no logic -- any branching here is encapsulated within the collaborators. 在此示例中要注意的一件事是,不仅您的处理程序中没有状态 ,而且也没有逻辑 -这里的任何分支都封装在协作者中。

Assuming the pieces fit together, there's not a lot that can fail that is the responsibility of the handler. 假设各个部分组合在一起,那么处理程序的责任就不是很多可能失败的事情。 So I wouldn't block a pull request like this because there wasn't a "unit test" for the handler. 因此,我不会阻止这样的请求请求,因为处理程序没有“单元测试”。

Could you clarify what you mean by: "Assuming the pieces fit together". 您能否阐明您的意思是:“假设各部分组合在一起”。 I 一世

In a "strongly typed" language, we would get a lot of checking done for us by the compiler/interpreter. 在“强类型”语言中,编译器/解释器将为我们完成大量检查。 If the type system isn't satisfied, then we are going to get an error message before the method is loaded into the execution environment. 如果不满意类型系统,那么在将方法加载到执行环境之前,我们将收到一条错误消息。

I see that you are adding/raising new events using .DispatchDomainEventsAsync(). 我看到您正在使用.DispatchDomainEventsAsync()添加/引发新事件。 Instead of returning nothing, you can return these events and write a simple unit test to count the number or structure these events. 您可以返回这些事件并编写一个简单的单元测试来计算这些事件的数量或结构,而不是什么也不返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM