Rhino Mock Stub异步方法

gle*_*eng 12 nunit unit-testing asynchronous rhino-mocks c#-5.0

我有一个ViewModel,它在构造函数中调用异步void方法来添加到集合中

public MyViewModel(ICommandHandler commandHandler)
{
    _commandHandler = commandHandler;
    SetupCollection();
}


private async void SetupCollection()
{
    var commands = GetCommands();

    foreach (var command in commands)
    {
        var response = await _commandHandler.ExecuteGetReply(command);

        if (response != null)
           Response.Add(response);
    }
}
Run Code Online (Sandbox Code Playgroud)

我究竟如何将_commandHandler.ExecuteGetReply()命令存根以返回值?

另外,在构造函数中有这样的函数可以做这样的事情吗?或者这可能是在... override void OnActivate()呼叫(我正在使用Caliburn Micro)?

Lee*_*Lee 22

ICommandHandler.ExecuteGetReply似乎返回一个Task<Response>所以你可以这样做:

ICommand commandArg;
Response response;
stubHandler.Stub(h => h.ExecuteGetReply(commandArg)).Return(Task.FromResult(response));
Run Code Online (Sandbox Code Playgroud)

但是,我不会从构造函数中调用异步void方法,因为在完成时无法通知您.