public async Task Get_HttpThrowsException_ThrowsException() { httpMessageHandlerMocked.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ThrowsAsync(new Exception("Mock exception")); var client = new HttpClient(httpMessageHandlerMocked.Object) { BaseAddress = new Uri("http://test") }; var service = new DungeonsApiService(client); await Assert.ThrowsAsync <Exception>(() => service.Get()); }
public async void Get_ReturnsListOfDungeons() { List <Dungeon> dungeons = new List <Dungeon> { new Dungeon { Number = -2, Name = "First dungeon" }, new Dungeon { Number = -1, Name = "Darkest dungeon" }, new Dungeon { Number = 0, Name = "THE DUNGEON" }, }; var dungeonJson = JsonSerializer.Serialize(dungeons); var httpContent = new StringContent(dungeonJson); var httpResponse = new HttpResponseMessage { Content = httpContent }; mockHttpMessageHandler(httpResponse); var httpClient = new HttpClient(httpMessageHandlerMocked.Object) { BaseAddress = new Uri("http://localhost/sfservice/") }; var service = new DungeonsApiService(httpClient); var result = await service.Get(); bool dungeonListsEqual = false; if (dungeons.Count == result.Count) { dungeonListsEqual = true; for (int i = 0; i < result.Count; i++) { if (!result[i].Equals(dungeons[i])) { dungeonListsEqual = false; break; } } } Assert.True(dungeonListsEqual); }
public async void Get_ReceivesApiError_ThrowsUnsuccessfulApiStatusCodeException() { var httpResponse = new HttpResponseMessage() { StatusCode = System.Net.HttpStatusCode.BadRequest }; mockHttpMessageHandler(httpResponse); var httpClient = new HttpClient(httpMessageHandlerMocked.Object) { BaseAddress = new Uri("http://test") }; var service = new DungeonsApiService(httpClient); await Assert.ThrowsAsync <UnsuccessfullApiStatusCodeException>(() => service.Get()); }
public async Task Get_ReturnsWrongHttpContent_ThrowsJsonException() { string content = "different content than list<dungeon>"; var httpContent = new StringContent(content); var httpResponse = new HttpResponseMessage { Content = httpContent }; mockHttpMessageHandler(httpResponse); var client = new HttpClient(httpMessageHandlerMocked.Object) { BaseAddress = new Uri("http://test") }; var service = new DungeonsApiService(client); await Assert.ThrowsAsync <JsonException>(() => service.Get()); }