RhinoMocks Testing callback method

Posted by joblot on Stack Overflow See other posts from Stack Overflow or by joblot
Published on 2010-05-28T00:05:51Z Indexed on 2010/05/28 0:11 UTC
Read the original article Hit count: 580

Hi All

I have a service proxy class that makes asyn call to service operation. I use a callback method to pass results back to my view model.

Doing functional testing of view model, I can mock service proxy to ensure methods are called on the proxy, but how can I ensure that callback method is called as well?

With RhinoMocks I can test that events are handled and event raise events on the mocked object, but how can I test callbacks?

ViewModel:

public class MyViewModel
{
    public void GetDataAsync()
    {
        // Use DI framework to get the object
        IMyServiceClient myServiceClient = IoC.Resolve<IMyServiceClient>();
        myServiceClient.GetData(GetDataAsyncCallback);
    }

    private void GetDataAsyncCallback(Entity entity, ServiceError error)
    {
        // do something here...
    }

}

ServiceProxy:

public class MyService : ClientBase, IMyServiceClient { // Constructor public NertiAdminServiceClient(string endpointConfigurationName, string remoteAddress) : base(endpointConfigurationName, remoteAddress) { }

// IMyServiceClient member.
public void GetData(Action<Entity, ServiceError> callback)
{
    Channel.BeginGetData(EndGetData, callback);
}

private void EndGetData(IAsyncResult result)
{
    Action<Entity, ServiceError> callback =
        result.AsyncState as Action<Entity, ServiceError>;

    ServiceError error;
    Entity results = Channel.EndGetData(out error, result);

    if (callback != null)
        callback(results, error);
}

}

Thanks

© Stack Overflow or respective owner

Related posts about rhino-mocks

Related posts about rhino-mocks-3.5