public void DeleteTransactionDeletesTransaction() { int accountIdForTest = 1; //Arrange: // Instantiate TransactionsController so its methods can be called // Create a new transaction to be deleted, and get its transaction ID var transactionController = new TransactionsController(); var transaction = new TransactionModel { AccountId = accountIdForTest, Amount = 3451.87M, TransactionDate = DateTime.Now }; IHttpActionResult result = transactionController.PostTransaction(transaction); CreatedAtRouteNegotiatedContentResult<TransactionModel> contentResult = (CreatedAtRouteNegotiatedContentResult<TransactionModel>)result; int transactionIdToDelete = contentResult.Content.TransactionId; //Act: Call DeleteTransaction result = transactionController.DeleteTransaction(transactionIdToDelete); //Assert: // Verify that HTTP result is OK // Verify that reading deleted transaction returns result not found Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<Transaction>)); result = transactionController.GetTransaction(transactionIdToDelete); Assert.IsInstanceOfType(result, typeof(NotFoundResult)); }
[TestMethod] //{2} public void GetTransactionReturnTransaction() { //Arrange var transactionController = new TransactionsController(); //Act IHttpActionResult result = transactionController.GetTransaction(1); //Assert //If not found Assert.IsNotInstanceOfType(result, typeof(NotFoundResult)); //If is found, and returned Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<TransactionModel>)); }
public void GetTransactionReturnsTransaction() { int transactionIdForTest = 1; //Arrange: Instantiate TransactionsController so its methods can be called var transactionController = new TransactionsController(); //Act: Call the GetTransaction method IHttpActionResult result = transactionController.GetTransaction(transactionIdForTest); //Assert: // Verify that HTTP status code is OK // Verify that returned transaction ID is correct Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<TransactionModel>)); OkNegotiatedContentResult<TransactionModel> contentResult = (OkNegotiatedContentResult<TransactionModel>)result; Assert.IsTrue(contentResult.Content.TransactionId == transactionIdForTest); }
public void PutTransactionUpdatesTransaction() { int transactionIdForTest = 1; decimal newTransactionAmount; //Arrange: Instantiate TransactionsController so its methods can be called var transactionController = new TransactionsController(); //Act: // Get an existing transaction, change it, and // pass it to PutTransaction IHttpActionResult result = transactionController.GetTransaction(transactionIdForTest); OkNegotiatedContentResult<TransactionModel> contentResult = (OkNegotiatedContentResult<TransactionModel>)result; TransactionModel updatedTransaction = (TransactionModel)contentResult.Content; decimal amountBeforeUpdate = updatedTransaction.Amount; updatedTransaction.Amount += 500.23M; newTransactionAmount = updatedTransaction.Amount; result = transactionController.PutTransaction (updatedTransaction.TransactionId, updatedTransaction); //Assert: // Verify that HTTP status code is OK // Get the transaction and verify that it was updated var statusCode = (StatusCodeResult)result; Assert.IsTrue(statusCode.StatusCode == System.Net.HttpStatusCode.NoContent); result = transactionController.GetTransaction(transactionIdForTest); Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<TransactionModel>)); OkNegotiatedContentResult<TransactionModel> readContentResult = (OkNegotiatedContentResult<TransactionModel>)result; updatedTransaction = (TransactionModel)readContentResult.Content; Assert.IsTrue(updatedTransaction.Amount == newTransactionAmount); updatedTransaction.Amount = amountBeforeUpdate; /* updatedTransaction.Amount = 1000M; */ result = transactionController.PutTransaction (updatedTransaction.TransactionId, updatedTransaction); }
[TestMethod] //{4} public void PutTransactionUpdateTransaction() { IHttpActionResult result; CreatedAtRouteNegotiatedContentResult<TransactionModel> contentResult; OkNegotiatedContentResult<TransactionModel> transactionResult; //Arrange using (var transactionsController = new TransactionsController()) { var tsModel = new TransactionModel { Amount = 123, }; result = transactionsController.PostTransaction(tsModel); //Cast result as Content Result so that I can gather information from ContentResult contentResult = (CreatedAtRouteNegotiatedContentResult<TransactionModel>)result; } using (var secondTransactionsController = new TransactionsController()) { //Result contains the customer I had JUST createad result = secondTransactionsController.GetTransaction(1); Assert.IsNotInstanceOfType(result, typeof(NotFoundResult)); Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<TransactionModel>)); //Get transactionModel from 'result' transactionResult = (OkNegotiatedContentResult<TransactionModel>)result; } using (var thirdTransactionsController = new TransactionsController()) { var modifiedContent = transactionResult.Content; modifiedContent.Amount += 5; //Act result = thirdTransactionsController.PutTransaction(transactionResult.Content.TransactionId, modifiedContent); //Assert Assert.IsInstanceOfType(result, typeof(StatusCodeResult)); } }
[TestMethod] //{6} public void DeleteTransactionDeleteTransaction() { //Arrange //Create Controller var transactionsController = new TransactionsController(); //Create a customer to be deleted var dbTransactions = new TransactionModel { Amount = 21323, }; //Add 'new customer' to the DB using a POST //Save returned value as RESULT IHttpActionResult result = transactionsController.PostTransaction(dbTransactions); //Cast result as Content Result so that I can gather information from ContentResult CreatedAtRouteNegotiatedContentResult<TransactionModel> contentResult = (CreatedAtRouteNegotiatedContentResult<TransactionModel>)result; //Result contains the customer I had JUST created result = transactionsController.GetTransaction(1); //Get CustomerModel from 'result' OkNegotiatedContentResult<TransactionModel> customerResult = (OkNegotiatedContentResult<TransactionModel>)result; //Act //The result of the Delete Request IHttpActionResult second = transactionsController.DeleteTransaction(1); //Assert //If action returns: NotFound() Assert.IsNotInstanceOfType(second, typeof(NotFoundResult)); Assert.IsInstanceOfType(second, typeof(OkNegotiatedContentResult<TransactionModel>)); }