public async Task PrimaryObjectsController_GetAllAsync_NotFound() { // This test verifies that the controller does not throw an error when getting all objects and null is returned from the service var primaryObjectsController = new PrimaryObjectsController(_primaryObjectService.Object); ICollection <DomainModels.PrimaryObject> source = null; _primaryObjectService.Setup(_ => _.GetAllAsync()).ReturnsAsync(source); try { await primaryObjectsController.GetAllAsync(); Assert.Fail(); } catch (HttpResponseException ex) { Assert.IsNotNull(ex.Response); Assert.AreEqual(HttpStatusCode.NotFound, ex.Response.StatusCode); } }
public async Task PrimaryObjectsController_GetAllAsync_Found() { // This test verifies that the controller does not throw an error when getting all objects var primaryObjectsController = new PrimaryObjectsController(_primaryObjectService.Object); var source = new List <DomainModels.PrimaryObject>() { 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", }, }, }, new DomainModels.PrimaryObject(Guid.NewGuid()) { Description = "Description 2", Name = "Name 2", SecondaryObjects = new List <DomainModels.SecondaryObject>() { new DomainModels.SecondaryObject(Guid.NewGuid()) { Description = "Description 2.1", Name = "Name 2.1", }, new DomainModels.SecondaryObject(Guid.NewGuid()) { Description = "Description 2.2", Name = "Name 2.2", }, }, }, }; foreach (var sourcePrimaryObject in source) { foreach (var sourceSecondaryObject in sourcePrimaryObject.SecondaryObjects) { sourceSecondaryObject.PrimaryObject = sourcePrimaryObject; sourceSecondaryObject.PrimaryObject_Id = sourcePrimaryObject.Id; } } _primaryObjectService.Setup(_ => _.GetAllAsync()).ReturnsAsync(source); var destination = await primaryObjectsController.GetAllAsync(); Assert.IsNotNull(destination); foreach (var destinationPrimaryObject in destination) { var sourcePrimaryObject = source.SingleOrDefault(_ => _.Id == destinationPrimaryObject.Id); Assert.IsNotNull(source); 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); } } }