public void PutCustomerUpdatesCustomer() { //Arrange var cusomersContoller = new CustomersController(); var newCustomer = new CustomerModel { FirstName = "Testy", LastName = "McTesterson", Address1 = "123 Main Street", Address2 = "Suite 2", City = "San Diego", States = "CA", Zip = "92101" }; //The result of the Post Request IHttpActionResult result = cusomersContoller.PostCustomer(newCustomer); //Cast result as Content Result so that I can gather information from ContentResult CreatedAtRouteNegotiatedContentResult<CustomerModel> contentResult = (CreatedAtRouteNegotiatedContentResult<CustomerModel>)result; //Result contains the customer I had JUST createad result = cusomersContoller.GetCustomer(contentResult.Content.CustomerId); //Get CustomerModel from 'result' OkNegotiatedContentResult<CustomerModel> customerResult = (OkNegotiatedContentResult<CustomerModel>)result; //Act //The result of the Put Request result = cusomersContoller.PutCustomer(customerResult.Content.CustomerId, newCustomer); //Assert Assert.IsInstanceOfType(result, typeof(StatusCodeResult)); }
public void PutCustomerUpdatesCustomer() { int customerIdForTest = 1; //Arrange: Instantiate CustomersController so its methods can be called var customerController = new CustomersController(); //Act: // Get an existing customer, change it, and // pass it to PutCustomer IHttpActionResult result = customerController.GetCustomer(customerIdForTest); OkNegotiatedContentResult<CustomerModel> contentResult = (OkNegotiatedContentResult<CustomerModel>)result; CustomerModel updatedCustomer = (CustomerModel)contentResult.Content; string address1BeforeUpdate = updatedCustomer.Address1; string cityBeforeUpdate = updatedCustomer.City; string stateBeforeUpdate = updatedCustomer.State; string zipBeforeUpdate = updatedCustomer.Zip; updatedCustomer.Address1 = "567 State Street"; updatedCustomer.City = "Philadelphia"; updatedCustomer.State = "PA"; updatedCustomer.Zip = "14378"; result = customerController.PutCustomer (updatedCustomer.CustomerId, updatedCustomer); //Assert: // Verify that HTTP status code is OK // Get the customer and verify that it was updated var statusCode = (StatusCodeResult)result; Assert.IsTrue(statusCode.StatusCode == System.Net.HttpStatusCode.NoContent); result = customerController.GetCustomer(customerIdForTest); Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<CustomerModel>)); OkNegotiatedContentResult<CustomerModel> readContentResult = (OkNegotiatedContentResult<CustomerModel>)result; updatedCustomer = (CustomerModel)readContentResult.Content; Assert.IsTrue(updatedCustomer.Address1 == "567 State Street"); Assert.IsTrue(updatedCustomer.City == "Philadelphia"); Assert.IsTrue(updatedCustomer.State == "PA"); Assert.IsTrue(updatedCustomer.Zip == "14378"); updatedCustomer.Address1 = address1BeforeUpdate; updatedCustomer.City = cityBeforeUpdate; updatedCustomer.State = stateBeforeUpdate; updatedCustomer.Zip = zipBeforeUpdate; /* updatedCustomer.Address1 = "123 Fake Street"; updatedCustomer.City = "San Diego"; updatedCustomer.State = "CA"; updatedCustomer.Zip = "92101"; */ result = customerController.PutCustomer (updatedCustomer.CustomerId, updatedCustomer); }