I am attempting to unit test the interactions of my application and the asynchronous HttpWebRequest methods and am wondering how to simulate the call of the asynchronous callback. For instance, consider the following code:
Task.Factory
.FromAsync<Stream>(wr.BeginGetRequestStream, wr.EndGetRequestStream, null)
.ContinueWith(SomeDelegate);
In this case wr is an object that essentially wraps HttpWebRequest. I can stub and set expectations on this object in the unit test, but I can not think of a way to create the signal that would otherwise call EndRequestStream. Without this signal, the unit test blocks indefinitely waiting for the task to complete, which will never happen.
I can create interfaces and proxies for Task and TaskFactory, then set expectations at that level, but I am hoping there is something less involved.
Can you change the type of
wrto expose aGetRequestAsyncmethod that returns aTask<Stream>? That way you can mock it so that it returns a task from aTaskCompletionSourcewhich you can prod within your test. I’ve done something similar with a class myself to test a composition block, and it worked well. The tests are interesting to write, in terms of all the different things that can happen, but it works…