You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Pure Krome edited this page Dec 23, 2016
·
4 revisions
Faking an exception
What: Throwing a network exception when the HttpClient attempts to retrieve some data.
Why: Sometimes a severe network error might occur and an exception could be thrown. This is how you can test for that scenario.
How: Create a fake message handler that will throw an exception.
[Fact]publicasyncTaskGivenAValidHttpRequest_GetSomeDataAsync_ReturnsAFoo(){// Arrange.conststringerrorMessage="Oh man - something bad happened.";varexpectedException=newHttpRequestException(errorMessage);varmessageHandler=newFakeHttpMessageHandler(expectedException);varmyService=newMyService(messageHandler);// Act.// NOTE: network traffic will not leave your computer because you've faked the response, above.varexception=awaitShould.ThrowAsync<HttpRequestException>(myService.GetSomeDataAsync());// Assert.exception.Message.ShouldBe(errorMessage);}