Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rhino Mock Stub Async Method

I have a ViewModel which, in the constructor, makes a call to an async void method to add to a collection

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);
    }
}

How exactly would I stub the _commandHandler.ExecuteGetReply() command to return a value?

Also, is it OK to have such a function in the constructor to do something like this? Or should this perhaps go within an... override void OnActivate() call (I'm using Caliburn Micro) ?

like image 310
gleng Avatar asked Aug 22 '13 17:08

gleng


1 Answers

ICommandHandler.ExecuteGetReply appears to return a Task<Response> so you can do something like:

ICommand commandArg;
Response response;
stubHandler.Stub(h => h.ExecuteGetReply(commandArg)).Return(Task.FromResult(response));

I wouldn't call an async void method from your constructor however, since you will have no way of being notified when it has completed.

like image 174
Lee Avatar answered Nov 15 '22 06:11

Lee