public async Task PrimaryObjectsController_UpdateAsync_Found() { // This test verifies that the controller does not throw an error when updating a valid object var primaryObjectsController = new PrimaryObjectsController(_primaryObjectService.Object); var sourcePrimaryObject = new DomainModels.PrimaryObject(Guid.NewGuid()) { Description = "Description 1", Name = "Name 1", SecondaryObjects = new List <DomainModels.SecondaryObject>() { new DomainModels.SecondaryObject(Guid.NewGuid()) { Description = "Description 1.1", Name = "Name 1.1", }, new DomainModels.SecondaryObject(Guid.NewGuid()) { Description = "Description 1.2", Name = "Name 1.2", }, }, }; foreach (var sourceSecondaryObject in sourcePrimaryObject.SecondaryObjects) { sourceSecondaryObject.PrimaryObject = sourcePrimaryObject; sourceSecondaryObject.PrimaryObject_Id = sourcePrimaryObject.Id; } _primaryObjectService.Setup(_ => _.UpdateAsync(sourcePrimaryObject.Id, It.IsAny <ApiModels.PrimaryObject>())).ReturnsAsync(sourcePrimaryObject); var destinationPrimaryObject = await primaryObjectsController.UpdateAsync(sourcePrimaryObject.Id, new ApiModels.PrimaryObject()); Assert.IsNotNull(destinationPrimaryObject); Assert.AreEqual(sourcePrimaryObject.Description, destinationPrimaryObject.Description); Assert.AreEqual(sourcePrimaryObject.Id, destinationPrimaryObject.Id); Assert.AreEqual(sourcePrimaryObject.Name, destinationPrimaryObject.Name); Assert.IsNotNull(destinationPrimaryObject.SecondaryObjects); Assert.AreEqual(sourcePrimaryObject.SecondaryObjects.Count, destinationPrimaryObject.SecondaryObjects.Count()); foreach (var destinationSecondaryObject in destinationPrimaryObject.SecondaryObjects) { var sourceSecondaryObject = sourcePrimaryObject.SecondaryObjects.SingleOrDefault(_ => _.Id == destinationSecondaryObject.Id); Assert.IsNotNull(sourceSecondaryObject); Assert.IsNull(destinationSecondaryObject.Description); Assert.AreEqual(sourceSecondaryObject.Id, destinationSecondaryObject.Id); Assert.IsNull(destinationSecondaryObject.Name); Assert.IsNull(destinationSecondaryObject.PrimaryObject); } }
public async Task PrimaryObjectsController_UpdateAsync_NotFound() { // This test verifies that the controller throws the correct HttpResponseException when a non-existant object is updated var primaryObjectsController = new PrimaryObjectsController(_primaryObjectService.Object); DomainModels.PrimaryObject sourcePrimaryObject = null; _primaryObjectService.Setup(_ => _.UpdateAsync(It.IsAny <Guid>(), It.IsAny <ApiModels.PrimaryObject>())).ReturnsAsync(sourcePrimaryObject); try { await primaryObjectsController.UpdateAsync(Guid.NewGuid(), new ApiModels.PrimaryObject()); Assert.Fail(); } catch (HttpResponseException ex) { Assert.IsNotNull(ex.Response); Assert.AreEqual(HttpStatusCode.NotFound, ex.Response.StatusCode); } }