public async Task ShouldGetPHN() { Patient expected = new Patient("1234", "000", "Test", "Gateway"); 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); Mock <IAuthService> authMock = new Mock <IAuthService>(); /*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);*/ IPatientService service = new RestPatientService(httpMock.Object, configuration, authMock.Object); string phn = await service.GetPatientPHNAsync(expected.HdId); Assert.Equal(expected.PersonalHealthNumber, phn); }
public async Task ShouldCatchBadRequest() { 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)); }); Mock <IAuthService> authMock = new Mock <IAuthService>(); var client = new HttpClient(clientHandlerStub); httpMock.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(client); IPatientService service = new RestPatientService(httpMock.Object, configuration, authMock.Object); HttpRequestException ex = await Assert.ThrowsAsync <HttpRequestException>(() => service.GetPatientPHNAsync("")); }