Esempio n. 1
0
        public async Task TestGetStarships2()
        {
            var secondPageUrl = "Second_Page_Url";
            var pageCount     = StarshipApiServiceMock.Starships.Count / 2 + 1;

            // Init mock for 2 pages
            var mock = new Mock <IApiService>();

            mock.Setup(x => x.GetRequestAsync <StarshipsResponse>(secondPageUrl)).Returns(Task.FromResult(new StarshipsResponse()
            {
                Count     = StarshipApiServiceMock.Starships.Count,
                Starships = StarshipApiServiceMock.Starships.Skip(pageCount).ToList()
            }));
            mock.Setup(x => x.GetRequestAsync <StarshipsResponse>(It.IsNotIn(secondPageUrl))).Returns(Task.FromResult(new StarshipsResponse()
            {
                Count       = StarshipApiServiceMock.Starships.Count,
                Starships   = StarshipApiServiceMock.Starships.Take(pageCount).ToList(),
                NextPageUrl = secondPageUrl
            }));

            var mockService = new SWApiService(mock.Object);
            var starships   = await mockService.GetAllStarships();

            Assert.NotEmpty(starships);
            Assert.Equal(StarshipApiServiceMock.Starships.Count, starships.Count);

            // Order should match for both parallel and sequantional request
            for (int i = 0; i < StarshipApiServiceMock.Starships.Count; i++)
            {
                Assert.True(StarshipsEqual(starships[i], StarshipApiServiceMock.Starships[i]));
            }
        }
Esempio n. 2
0
        public async Task TestGetStarshipsSequentially()
        {
            var result = await service.GetAllStarships();

            Assert.NotEmpty(result);
            Assert.Equal(result.Count, StarshipApiServiceMock.Starships.Count);
        }
Esempio n. 3
0
        public async Task TestStarshipParallelGetOrder()
        {
            var slowService = new SWApiService(new StarshipSlowApiServiceMock());
            var starships   = await slowService.GetAllStarships();

            var starships2 = await slowService.GetAllStarshipsParallelly();

            Assert.Equal(starships.Count, starships2.Count);
            for (int i = 0; i < starships.Count; i++)
            {
                Assert.True(StarshipsEqual(starships[i], starships2[i]));
            }
        }
Esempio n. 4
0
        private async Task TestStarshipsCollection(StarshipsResponse response, Action <List <Starship> > test)
        {
            var mock = new Mock <IApiService>();

            mock.Setup(x => x.GetRequestAsync(It.IsNotNull <string>())).Returns(Task.FromResult(JsonConvert.SerializeObject(response)));
            mock.Setup(x => x.GetRequestAsync <StarshipsResponse>(It.IsNotNull <string>())).Returns(Task.FromResult(response));

            var mockService = new SWApiService(mock.Object);
            var starships   = await mockService.GetAllStarships();

            var starships2 = await mockService.GetAllStarshipsParallelly();

            test.Invoke(starships);
            test.Invoke(starships2);
        }
Esempio n. 5
0
        public async Task TestGetStarshipsSequentially()
        {
            var result = await service.GetAllStarships();

            Assert.NotEmpty(result);
        }