Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.05 KB | None | 0 0
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using TestBank.Models;
  4. using TestBank.Repository;
  5.  
  6. namespace BankTests
  7. {
  8.     [TestClass]
  9.     public class AccountRepositoryTests
  10.     {
  11.         #region Debit
  12.  
  13.         /// <summary>
  14.         ///     Debit should update the account balance correctly.
  15.         /// </summary>
  16.         [TestMethod]
  17.         public void Debit_WithValidAmount_UpdatesBalance()
  18.         {
  19.             // Arrange
  20.             var startBalance = 170.65;
  21.             var endBalance = 145.54;
  22.             var amount = 25.11;
  23.  
  24.             // Create account
  25.             var account = new Account
  26.             {
  27.                 Balance = startBalance,
  28.                 IsFrozen = false
  29.             };
  30.  
  31.             // Create repository and add account
  32.             var repository = new AccountRepository();
  33.             repository.Add(account);
  34.  
  35.             // Change
  36.             repository.Debit(account, amount);
  37.  
  38.             // Assert
  39.             Assert.AreEqual(endBalance, account.Balance, 0.001, "Debit error");
  40.         }
  41.  
  42.  
  43.         /// <summary>
  44.         ///     Debit should throw the ArgumentOutOfRange exception, when amount is lower than 0.
  45.         /// </summary>
  46.         [TestMethod]
  47.         [ExpectedException(typeof(ArgumentOutOfRangeException))]
  48.         public void Debit_WithInvalidAmount_ShouldThrowArgumentOutOfRange()
  49.         {
  50.             // Arrange
  51.             var startBalance = 20.00;
  52.             var amount = -5.00;
  53.  
  54.             // Create account
  55.             var account = new Account
  56.             {
  57.                 Balance = startBalance,
  58.                 IsFrozen = false
  59.             };
  60.  
  61.             // Create repository
  62.             var repository = new AccountRepository();
  63.             repository.Add(account);
  64.  
  65.             // Change
  66.             repository.Debit(account, amount);
  67.         }
  68.  
  69.         #endregion
  70.  
  71.         #region Credit
  72.  
  73.         /// <summary>
  74.         ///     Credit should update the account balance correctly.
  75.         /// </summary>
  76.         [TestMethod]
  77.         public void Credit_WithValidAmount_UpdatesBalance()
  78.         {
  79.             // Arrange
  80.             var startBalance = 130.50;
  81.             var endBalance = 145.54;
  82.             var amount = 15.04;
  83.  
  84.             // Create account
  85.             var account = new Account
  86.             {
  87.                 Balance = startBalance,
  88.                 IsFrozen = false
  89.             };
  90.  
  91.             // Create repository and add account
  92.             var repository = new AccountRepository();
  93.             repository.Add(account);
  94.  
  95.             // Change
  96.             repository.Credit(account, amount);
  97.  
  98.             // Assert
  99.             Assert.AreEqual(endBalance, account.Balance, 0.001, "Debit error");
  100.         }
  101.  
  102.         /// <summary>
  103.         ///     Credit should throw the ArgumentOutOfRange exception, when amount is lower than 0.
  104.         /// </summary>
  105.         [TestMethod]
  106.         [ExpectedException(typeof(ArgumentOutOfRangeException))]
  107.         public void Credit_WithInvalidAmount_ShouldThrowArgumentOutOfRange()
  108.         {
  109.             // Arrange
  110.             var startBalance = 20.00;
  111.             var amount = -5.00;
  112.  
  113.             // Create account
  114.             var account = new Account
  115.             {
  116.                 Balance = startBalance,
  117.                 IsFrozen = false
  118.             };
  119.  
  120.             // Create repository
  121.             var repository = new AccountRepository();
  122.             repository.Add(account);
  123.  
  124.             // Change
  125.             repository.Credit(account, amount);
  126.         }
  127.  
  128.         #endregion
  129.  
  130.         #region Transfer
  131.  
  132.         /// <summary>
  133.         ///     Transfer with valid amount should update balance.
  134.         /// </summary>
  135.         [TestMethod]
  136.         public void Transfer_WithValidAmount_UpdatesBalance()
  137.         {
  138.             // Arrange
  139.             var amount = 15.04;
  140.             var fromStartBalance = 145.54;
  141.             var fromEndBalance = 130.50;
  142.             var toStartBalance = 20.00;
  143.             var toEndBalance = 35.04;
  144.  
  145.             // Create accounts
  146.             var fromAccount = new Account
  147.             {
  148.                 Balance = fromStartBalance,
  149.                 IsFrozen = false
  150.             };
  151.             var toAccount = new Account
  152.             {
  153.                 Balance = toStartBalance,
  154.                 IsFrozen = false
  155.             };
  156.  
  157.             // Create repository and add account
  158.             var repository = new AccountRepository();
  159.             repository.Add(fromAccount);
  160.             repository.Add(toAccount);
  161.  
  162.             // Change
  163.             repository.Transfer(toAccount, fromAccount, amount);
  164.  
  165.             // Assert
  166.             Assert.AreEqual(fromEndBalance, fromAccount.Balance, 0.001, "Debit error");
  167.             Assert.AreEqual(toEndBalance, toAccount.Balance, 0.001, "Debit error");
  168.         }
  169.  
  170.         /// <summary>
  171.         ///     Transfer should throw ArgumentOutOfRangeException
  172.         ///     exception when amount is 0 or under.
  173.         /// </summary>
  174.         [TestMethod]
  175.         [ExpectedException(typeof(ArgumentOutOfRangeException))]
  176.         public void Transfer_WithInValidAmount_ShouldThrowArgumentOutOfRange()
  177.         {
  178.             // Arrange
  179.             var amount = -15.04;
  180.             var fromStartBalance = 145.54;
  181.             var toStartBalance = 20.00;
  182.  
  183.             // Create accounts
  184.             var fromAccount = new Account
  185.             {
  186.                 Balance = fromStartBalance,
  187.                 IsFrozen = false
  188.             };
  189.             var toAccount = new Account
  190.             {
  191.                 Balance = toStartBalance,
  192.                 IsFrozen = false
  193.             };
  194.  
  195.             // Create repository and add account
  196.             var repository = new AccountRepository();
  197.             repository.Add(fromAccount);
  198.             repository.Add(toAccount);
  199.  
  200.             // Change
  201.             repository.Transfer(toAccount, fromAccount, amount);
  202.         }
  203.  
  204.  
  205.         /// <summary>
  206.         ///     Transfer from an account with a balance too
  207.         ///     low should throw ArgumentOutOfRangeException exception.
  208.         /// </summary>
  209.         [TestMethod]
  210.         [ExpectedException(typeof(ArgumentOutOfRangeException))]
  211.         public void Transfer_WithLowBalance_ShouldThrowArgumentOutOfRange()
  212.         {
  213.             // Arrange
  214.             var amount = 299.99;
  215.             var fromStartBalance = 145.54;
  216.             var toStartBalance = 20.00;
  217.  
  218.             // Create accounts
  219.             var fromAccount = new Account
  220.             {
  221.                 Balance = fromStartBalance,
  222.                 IsFrozen = false
  223.             };
  224.             var toAccount = new Account
  225.             {
  226.                 Balance = toStartBalance,
  227.                 IsFrozen = false
  228.             };
  229.  
  230.             // Create repository and add account
  231.             var repository = new AccountRepository();
  232.             repository.Add(fromAccount);
  233.             repository.Add(toAccount);
  234.  
  235.             // Change
  236.             repository.Transfer(toAccount, fromAccount, amount);
  237.         }
  238.  
  239.         #endregion
  240.     }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement