Loul G.
Loul G.

Reputation: 1105

Mocking a HttpClient TimeoutException

I'm unit testing my viewmodel command that call a web service. My viewmodel has a dependency on IHttpService which 'GetAsync' method is called by this viewmodel.

I want to unit test my viewmodel and manage the fact that 'GetAsync' returns a cancelled task, which is the case when it's implemented with HttpClient and when an TimeOutException occurs. How can i setup my IHttpServiceMock to return such a task ?

I tryed with TaskCompletionSource.SetCanceled() but i'm not able to tell with which exception it was cancelled...

Upvotes: 0

Views: 1767

Answers (1)

Andy
Andy

Reputation: 8562

I think you need to use SetException instead of SetCanceled; a thread that completes with an exception still completed, vs. a thread that was asked to stop working.

Upvotes: 1

Related Questions