コード例 #1
0
        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"));
        }
コード例 #2
0
        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"));
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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(""));
        }
コード例 #5
0
        protected override async Task OnParametersSetAsync()
        {
            var dungeonMonsters = await DungeonsApiService.GetDungeonWithMonstersById(DungeonNumber);

            DungeonMonsters = dungeonMonsters;
        }