public async Task TestCollectionsApiPutAsync() { // Find out how many collections there are // Arrange int actualCollectionCount = 0; // Act IHttpActionResult collectionResult = controller.Get(); var message = await collectionResult.ExecuteAsync(new System.Threading.CancellationToken()); var collections = await message.Content.ReadAsAsync <IEnumerable <Collection> >(); foreach (var collection in collections) { actualCollectionCount++; } ////////////////////////////////////////////////////////////////// // PUT a new collection with invalid model - expect bad request message Collection newCollection = new Collection(); newCollection.Name = ""; newCollection.Description = "Lots of things"; newCollection.Id = new Guid("55555555-cccc-1111-4444-111111111111"); controller.ModelState.AddModelError("k1", "Name is required"); IHttpActionResult result = controller.Put(new CollectionAPI(newCollection)); var badMessage = await result.ExecuteAsync(new System.Threading.CancellationToken()); Assert.AreEqual(HttpStatusCode.BadRequest, badMessage.StatusCode); // Make new collection's model valid and PUT newCollection.Name = "Things"; controller.ModelState.Remove("k1"); IHttpActionResult goodResult = controller.Put(new CollectionAPI(newCollection)); var goodMessage = await goodResult.ExecuteAsync(new System.Threading.CancellationToken()); Assert.AreEqual(HttpStatusCode.OK, goodMessage.StatusCode); ///////////////////////////////////////////////////////////////// // Expect collection count to be one more than before // Arrange int expectedCollectionCount = actualCollectionCount + 1; // reset count to test again after the PUT actualCollectionCount = 0; // Act collectionResult = controller.Get(); message = await collectionResult.ExecuteAsync(new System.Threading.CancellationToken()); collections = await message.Content.ReadAsAsync <IEnumerable <Collection> >(); foreach (var collection in collections) { actualCollectionCount++; } // Assert Assert.AreEqual(expectedCollectionCount, actualCollectionCount); ///////////////////////////////////////////////////////////////// // Now do an update to existing collection with PUT // collection count shouldn't change // Arrange expectedCollectionCount = actualCollectionCount; // reset count to test again after the PUT actualCollectionCount = 0; Collection oldCollection = new Collection(); oldCollection.Name = "Things"; oldCollection.Description = "All the things."; oldCollection.Id = new Guid("55555555-cccc-1111-4444-111111111111"); goodResult = controller.Put(new CollectionAPI(oldCollection)); goodMessage = await goodResult.ExecuteAsync(new System.Threading.CancellationToken()); Assert.AreEqual(HttpStatusCode.OK, goodMessage.StatusCode); // Act collectionResult = controller.Get(); message = await collectionResult.ExecuteAsync(new System.Threading.CancellationToken()); collections = await message.Content.ReadAsAsync <IEnumerable <Collection> >(); foreach (var collection in collections) { actualCollectionCount++; } // Assert Assert.AreEqual(expectedCollectionCount, actualCollectionCount); }