public void PutAccountUpdateAccount() { IHttpActionResult result; CreatedAtRouteNegotiatedContentResult<AccountModel> contentResult; OkNegotiatedContentResult<AccountModel> accountResult; //Arrange using (var accountsController = new AccountsController()) { //Build new AccountModel Object var newAccount = new AccountModel { AccountNumber = 21323, Balance = 213213, }; //Insert AccountModelObject into Database so //that I can take it out and test for update. result = accountsController.PostAccount(newAccount); //Cast result as Content Result so that I can gather information from ContentResult contentResult = (CreatedAtRouteNegotiatedContentResult<AccountModel>)result; } using (var SecondaccountsController = new AccountsController()) { //Result contains the customer I had JUST createad result = SecondaccountsController.GetAccount(contentResult.Content.AccountId); Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<AccountModel>)); //Get AccountModel from 'result' accountResult = (OkNegotiatedContentResult<AccountModel>)result; } using (var thirdController = new AccountsController()) { var modifiedAccount = accountResult.Content; modifiedAccount.Balance += 5; //Act //The result of the Put Request result = thirdController.PutAccount(accountResult.Content.AccountId, modifiedAccount); //Assert Assert.IsInstanceOfType(result, typeof(StatusCodeResult)); } }
public void PutAccountUpdatesAccount() { int accountIdForTest = 1; string accountNumberForTest = "XXXY1"; decimal balanceForTest = 872345.34M; //Arrange: Instantiate AccountsController so its methods can be called var accountController = new AccountsController(); //Act: // Get an existing account, change it, and // pass it to PutAccount IHttpActionResult result = accountController.GetAccount(accountIdForTest); OkNegotiatedContentResult<AccountModel> contentResult = (OkNegotiatedContentResult<AccountModel>)result; AccountModel updatedAccount = (AccountModel)contentResult.Content; string accountNumberBeforeUpdate = updatedAccount.AccountNumber; decimal balanceBeforeUpdate = updatedAccount.Balance; updatedAccount.AccountNumber = accountNumberForTest; updatedAccount.Balance = balanceForTest; result = accountController.PutAccount (updatedAccount.AccountId, updatedAccount); //Assert: // Verify that HTTP status code is OK // Get the account and verify that it was updated var statusCode = (StatusCodeResult)result; Assert.IsTrue(statusCode.StatusCode == System.Net.HttpStatusCode.NoContent); result = accountController.GetAccount(accountIdForTest); Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<AccountModel>)); OkNegotiatedContentResult<AccountModel> readContentResult = (OkNegotiatedContentResult<AccountModel>)result; updatedAccount = (AccountModel)readContentResult.Content; Assert.IsTrue(updatedAccount.AccountNumber == accountNumberForTest); Assert.IsTrue(updatedAccount.Balance == balanceForTest); updatedAccount.AccountNumber = accountNumberBeforeUpdate; updatedAccount.Balance = balanceBeforeUpdate; /* updatedAccount.AccountNumber = "1000"; updatedAccount.Balance = 938M; */ result = accountController.PutAccount (updatedAccount.AccountId, updatedAccount); }