How to Mock a Task<> Result?

Solution 1:

The simplest thing is just to return a completed task with the expected result:

var responseTask = Task.FromResult(response);

I imagine reason this hangs is that the mocked task is never started and hence the given func is not run. You could start it in your test:

var responseTask = MockRepository.GenerateMock<Task<HttpResponseMessage>>(taskFunc);
responseTask.Start();

However there's no reason to mock tasks since you can easily create completed/failed/cancelled tasks directly.