Esempio n. 1
0
        public async void CheckWhetherHelperReturnsExceptionWhenUrlIsNotSet()
        {
            var mockFactory = new Mock <IHttpClientFactory>();
            var mockConfig  = new Mock <IConfiguration>();
            List <OwnerModel> serviceResponse = new List <OwnerModel>();
            var configuration = new HttpConfiguration();
            Mock <ILogger <PetsService> > mockLoggerService = new Mock <ILogger <PetsService> >();
            var clientHandlerStub = new DelegatingHandlerStub((request, cancellationToken) => {
                request.SetConfiguration(configuration);
                var response = request.CreateResponse(HttpStatusCode.OK, serviceResponse);
                return(Task.FromResult(response));
            });
            var client = new Mock <HttpClient>();

            mockFactory.Setup(mock => mock.CreateClient(It.IsAny <string>())).Throws(new Exception());
            //  client.Setup(_ => _.SendAsync(It.IsAny<HttpRequestMessage>())).ReturnsAsync(new HttpResponseMessage());

            IHttpClientFactory factory = mockFactory.Object;

            PetsService service = new PetsService(factory, mockLoggerService.Object, mockConfig.Object);

            Task act() => service.GetOwnerPetsDetails(); await Assert.ThrowsAsync <Exception>(act);
        }
Esempio n. 2
0
        public async void CheckWhetherHelperReturningCorrectValue()
        {
            var mockFactory = new Mock <IHttpClientFactory>();
            var mockConfig  = new Mock <IConfiguration>();
            List <OwnerModel> serviceResponse = new List <OwnerModel>();
            var configuration = new HttpConfiguration();
            Mock <ILogger <PetsService> > mockLoggerService = new Mock <ILogger <PetsService> >();
            var clientHandlerStub = new DelegatingHandlerStub((request, cancellationToken) => {
                request.SetConfiguration(configuration);
                var response = request.CreateResponse(HttpStatusCode.OK, serviceResponse);
                return(Task.FromResult(response));
            });
            var client = new HttpClient();

            mockFactory.Setup(mock => mock.CreateClient(It.IsAny <string>())).Returns(client);

            IHttpClientFactory factory = mockFactory.Object;

            PetsService service = new PetsService(factory, mockLoggerService.Object, mockConfig.Object);

            var result = await service.GetOwnerPetsDetails();

            Assert.NotNull(result);
        }