I would like to write a unit test case by mocking the dependencies. The overall flow is as follows.
We have a WorklistLoader which has an async method LoadWorklistItemsAsync(). To accomplish this task WorklistLoader is dependent on lower layer API(which i want to mock) QueryManager.StartQueryTask(). StartQueryTask() is also a async method which queries the file system and raises the ProgressChanged() at regular intervals and then at the end raises the CompletedEvent. StartQueryTask() returns a reference to TPL Task.
Signature of the StartQueryTask is
Task StartQueryTask( "SomeId", EventHandler<ProgressChanged> progressChanged, EventHandler<QueryCompleted> queryCompleted); Once the WorklistLoader recieves the ProgressChanged event from QueryManager, it does some processing and then raises its ProgressChanged event(to which the ViewModel has subscribed).
I would like to test LoadWorklistItemsAsync() method of the WorklistLoader with mocking QueryManager.StartQueryTask().
Here are my questions.
- What is the best practice to write unit test for the
Async()methods with mocking? - How to write unit test case for methods whose dependencies uses TPL ?(methods returning
Tasktype)
Another question is
- If i mock my QueryManager.StartQueryTask() method using Rhinomocks How would it look like ? (The mocking code. It has to raise progresschanged, completed events and return Task).
WorklistLoaderin some way, regardless if the end method using the mock was async or sync. as for 2, I'd suggest you have a look at srtsolutions.com/testing-async-methods-in-c-5