The codebase I’m testing is making an extensive usage of the async pattern (BeginXXX, EndXXX).
Mocking async operations is somewhat cumbersome with the AAA API.
For the code flow to be appropriately faked, the BeginXXX must call the callback. Otherwise the test will hang if the code under test perform calls such as:
action.BeginXXX(paramA, result =>
{
action.EndXXX(result);
completedEvent.Set();
}, null);
What I’m currently doing is creating a mock object for each object that makes use of the async pattern.
Then I use the Isolate.Swap to fake calls on the original object:
Isolate.Swap.CallsOn(originalObject).WithCallsTo(fakedObject);
Each mock looks the same:
private Action m_action = () => { };
public IAsyncResult BeginXXX(String parameterA, HttpListenerContext context,
AsyncCallback callback, object asyncState)
{
return m_action.BeginInvoke(callback, asyncState);
}
public void EndXXX(IAsyncResult asyncResult)
{
m_action.EndInvoke(asyncResult);
}
Is there an easier way to do this? Maybe the previous generation API?
It could have been cool if Isolater could inspect method signatures of faked objects and automatically inject the code above.
Thanks,
Noam