Пример #1
0
        public async Task ShouldGetMedications()
        {
            HNMessage expected = new HNMessage()
            {
                Message = "Test",
                IsErr   = false,
            };

            Mock <IAuthService> authMock = new Mock <IAuthService>();

            authMock.Setup(s => s.ClientCredentialsAuth()).ReturnsAsync(new JWTModel());

            Mock <IHNMessageParser <MedicationStatement> > parserMock = new Mock <IHNMessageParser <MedicationStatement> >();

            parserMock.Setup(s => s.ParseResponseMessage(expected.Message)).Returns(new List <MedicationStatement>());

            Mock <IHttpClientFactory> httpMock = new Mock <IHttpClientFactory>();
            var clientHandlerStub = new DelegatingHandlerStub((request, cancellationToken) => {
                request.SetConfiguration(new HttpConfiguration());
                HttpResponseMessage response = request.CreateResponse(HttpStatusCode.OK, expected, MediaTypeNames.Application.Json);
                return(Task.FromResult(response));
            });
            var client = new HttpClient(clientHandlerStub);

            httpMock.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(client);

            IMedicationService         service     = new RestMedicationService(parserMock.Object, httpMock.Object, configuration, authMock.Object);
            List <MedicationStatement> medications = await service.GetMedicationsAsync("123456789", "test", "10.0.0.1");

            Assert.True(medications.Count == 0);
        }
Пример #2
0
        public async Task ShouldCatchBadRequest()
        {
            Mock <IAuthService> authMock = new Mock <IAuthService>();

            authMock.Setup(s => s.ClientCredentialsAuth()).ReturnsAsync(new JWTModel());

            Mock <IHNMessageParser <MedicationStatement> > parserMock = new Mock <IHNMessageParser <MedicationStatement> >();
            Mock <IHttpClientFactory> httpMock = new Mock <IHttpClientFactory>();
            var clientHandlerStub = new DelegatingHandlerStub((request, cancellationToken) => {
                request.SetConfiguration(new HttpConfiguration());
                HttpResponseMessage response = request.CreateResponse(HttpStatusCode.BadRequest);
                return(Task.FromResult(response));
            });
            var client = new HttpClient(clientHandlerStub);

            httpMock.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(client);
            IMedicationService   service = new RestMedicationService(parserMock.Object, httpMock.Object, configuration, authMock.Object);
            HttpRequestException ex      = await Assert.ThrowsAsync <HttpRequestException>(() => service.GetMedicationsAsync("", "", ""));
        }