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] //{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)); } }
public IHttpActionResult GetTransaction(int id) { // Find Transaction object corresponding to transaction ID Transaction dbTransaction = db.Transactions.Find(id); if (dbTransaction == null) { return NotFound(); } // Populate new TransactionModel object from Transaction object TransactionModel modelTransaction = new TransactionModel { AccountId = dbTransaction.AccountId, Amount = dbTransaction.Amount, TransactionDate = dbTransaction.TransactionDate, TransactionId = dbTransaction.TransactionId }; return Ok(modelTransaction); }
public void PostTransactionCreatesTransaction() { int accountIdForTest = 1; //Arrange: Instantiate TransactionsController so its methods can be called var transactionController = new TransactionsController(); //Act: // Create a TransactionModel object populated with test data, // and call PostTransaction var newTransaction = new TransactionModel { AccountId = accountIdForTest, Amount = -555M, TransactionDate = DateTime.Now }; IHttpActionResult result = transactionController.PostTransaction(newTransaction); //Assert: // Verify that the HTTP result is CreatedAtRouteNegotiatedContentResult // Verify that the HTTP result body contains a nonzero transaction ID Assert.IsInstanceOfType (result, typeof(CreatedAtRouteNegotiatedContentResult<TransactionModel>)); CreatedAtRouteNegotiatedContentResult<TransactionModel> contentResult = (CreatedAtRouteNegotiatedContentResult<TransactionModel>)result; Assert.IsTrue(contentResult.Content.TransactionId != 0); // Delete the test transaction result = transactionController.DeleteTransaction(contentResult.Content.TransactionId); }
public IHttpActionResult PostTransaction(TransactionModel transaction) { if (!ModelState.IsValid) { return BadRequest(ModelState); } // Set up new Transactio object, // and populate it with the values from // the input TransactionModel object Transaction dbTransaction = new Transaction(); dbTransaction.Update(transaction); // Add the new Transaction object to the list of Transaction objects db.Transactions.Add(dbTransaction); try { db.SaveChanges(); } catch (Exception) { throw new Exception("Unable to add the transaction to the database."); } // Update the TransactionModel object with the new transaction ID // that was placed in the Transaction object after the changes // were saved to the DB transaction.TransactionId = dbTransaction.TransactionId; return CreatedAtRoute("DefaultApi", new { id = dbTransaction.TransactionId }, transaction); }
public IHttpActionResult PutTransaction(int id, TransactionModel transaction) { // Validate the request if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != transaction.TransactionId) { return BadRequest(); } if (!TransactionExists(id)) { return BadRequest(); } // Get the transaction record corresponding to the transaction ID, then // update its properties to the values in the input TransactionModel object,, // and then set an indicator that the record has been modified var dbTransaction = db.Transactions.Find(id); dbTransaction.Update(transaction); db.Entry(dbTransaction).State = EntityState.Modified; // Perform update by saving changes to DB try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!TransactionExists(id)) { return NotFound(); } else { throw new Exception("Unable to update the transaction in the database."); } } return StatusCode(HttpStatusCode.NoContent); }
[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>)); }
[TestMethod] // {5} public void PostTransactionCreateTransactions() { //Arrange var transactionsController = new TransactionsController(); //Act var newTransaction = new TransactionModel { Amount = 12, }; IHttpActionResult result = transactionsController.PostTransaction(newTransaction); //Assert //If not 'true' Assert False Assert.IsInstanceOfType(result, typeof(CreatedAtRouteNegotiatedContentResult<TransactionModel>)); //Cast CreatedAtRouteNegotiatedContentResult<TransactionModel> contentResult = (CreatedAtRouteNegotiatedContentResult<TransactionModel>)result; //Check if Customer is posted to the database //Check to see if Customer ID is NOT equal to zero. If Customer Id us equal to zero, //then customer was NOT added to Database Assert.IsTrue(contentResult.Content.AccountId!= 0); }