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
TaskFactory, then set expectations at that level, but I am hoping there is something less involved.
Can you change the type of
wr to expose a
GetRequestAsync method that returns a
Task<Stream>? That way you can mock it so that it returns a task from a
TaskCompletionSource which 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...
I'm not sure I got queston right, if you want to simulate Event Raise you can use following syntax:
// wr should be Mock
// EndRequestStream - event
wr.Raise(x => x.EndRequestStream += null,
Do not forget to put right