public async Task GetAsync_EntityFound_Success() { const string entityId = "E0C3202D-8C3B-4934-AE19-7C0BB9C73DD4"; // arrange ClientServiceMockRepository.Setup( service => service.GetAsync<ClientModel>(It.Is<string>(value => value == entityId))) .Returns(Task.FromResult(new ClientModel() { Id = entityId })); ClientServiceMockRepository.Setup( service => service.GetAsync<ClientModel>(It.Is<string>(value => value != entityId))) .Throws(new EntityNotFoundException()); const string requestedUrl = "http://localhost/clients/" + entityId; // act using (var config = new HttpConfiguration()) { using (var request = new HttpRequestMessage(HttpMethod.Get, requestedUrl)) { request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, config); using (var controller = new ClientsController(MockApiServices, MockUnitOfWork, MockClientService, MockAccountService)) { controller.Request = request; controller.Configuration = config; using (HttpResponseMessage response = await controller.GetAsync(entityId)) { // assert Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); Assert.AreEqual(new Uri(requestedUrl), response.RequestMessage.RequestUri); ClientModel client = await response.Content.ReadAsAsync<ClientModel>(); Assert.IsNotNull(client); Assert.AreEqual(entityId, client.Id); } } } } }
public async Task GetAsync_EntityNotFound_ReturnNotFoundCode() { const string entityId = "E0C3202D-8C3B-4934-AE19-7C0BB9C73DD4"; // arrange ClientServiceMockRepository.Setup( service => service.GetAsync<ClientModel>(It.Is<string>(value => value == entityId))) .Returns(Task.FromResult(new ClientModel() {Id = entityId})); ClientServiceMockRepository.Setup( service => service.GetAsync<ClientModel>(It.Is<string>(value => value != entityId))) .Throws(new EntityNotFoundException()); const string requestedId = "3620BAA2-2D7E-40DE-942A-21E5F2933221"; const string requestedUrl = "http://localhost/clients/" + requestedId; // act using (var config = new HttpConfiguration()) { using (var request = new HttpRequestMessage(HttpMethod.Get, requestedUrl)) { request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, config); using (var controller = new ClientsController(MockApiServices, MockUnitOfWork, MockClientService, MockAccountService)) { controller.Request = request; controller.Configuration = config; using (HttpResponseMessage response = await controller.GetAsync(requestedId)) { // assert Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode); } } } } }