public async Task GetDungeonWithMonstersById_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.GetDungeonWithMonstersById("id")); }
public async Task GetDungeonWithMonstersById_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.GetDungeonWithMonstersById("id")); }
public async Task GetDungeonWithMonstersById_GetsDungeonId_ReturnsDungeonWithMonsters() { var resultList = new List <DungeonMonster> { new DungeonMonster { Class = "some class", Constitution = "123", Dexterity = "444", DungeonNumber = 1, Experience = "12" }, new DungeonMonster { Class = "some class", Constitution = "123", Dexterity = "444", DungeonNumber = 1, Experience = "12", Level = 91, Luck = "1233323" }, }; var resultListJson = JsonSerializer.Serialize(resultList); var httpContent = new StringContent(resultListJson); var httpResponse = new HttpResponseMessage() { Content = httpContent }; mockHttpMessageHandler(httpResponse); var client = new HttpClient(httpMessageHandlerMocked.Object) { BaseAddress = new Uri("http://test") }; var service = new DungeonsApiService(client); var result = await service.GetDungeonWithMonstersById("id"); bool resultListsEqual = false; if (result.Count == resultList.Count) { resultListsEqual = true; for (int i = 0; i < resultList.Count; i++) { if (!result[i].Equals(resultList[i])) { resultListsEqual = false; break; } } } Assert.True(resultListsEqual); }
public async Task GetDungeonWithMonstersById_ReturnsWrongHttpContent_ThrowsJsonException() { string content = "different content than list<dungeonmonster>"; 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.GetDungeonWithMonstersById("")); }
protected override async Task OnParametersSetAsync() { var dungeonMonsters = await DungeonsApiService.GetDungeonWithMonstersById(DungeonNumber); DungeonMonsters = dungeonMonsters; }