Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using TestBank.Models;
- using TestBank.Repository;
- namespace BankTests
- {
- [TestClass]
- public class AccountRepositoryTests
- {
- #region Debit
- /// <summary>
- /// Debit should update the account balance correctly.
- /// </summary>
- [TestMethod]
- public void Debit_WithValidAmount_UpdatesBalance()
- {
- // Arrange
- var startBalance = 170.65;
- var endBalance = 145.54;
- var amount = 25.11;
- // Create account
- var account = new Account
- {
- Balance = startBalance,
- IsFrozen = false
- };
- // Create repository and add account
- var repository = new AccountRepository();
- repository.Add(account);
- // Change
- repository.Debit(account, amount);
- // Assert
- Assert.AreEqual(endBalance, account.Balance, 0.001, "Debit error");
- }
- /// <summary>
- /// Debit should throw the ArgumentOutOfRange exception, when amount is lower than 0.
- /// </summary>
- [TestMethod]
- [ExpectedException(typeof(ArgumentOutOfRangeException))]
- public void Debit_WithInvalidAmount_ShouldThrowArgumentOutOfRange()
- {
- // Arrange
- var startBalance = 20.00;
- var amount = -5.00;
- // Create account
- var account = new Account
- {
- Balance = startBalance,
- IsFrozen = false
- };
- // Create repository
- var repository = new AccountRepository();
- repository.Add(account);
- // Change
- repository.Debit(account, amount);
- }
- #endregion
- #region Credit
- /// <summary>
- /// Credit should update the account balance correctly.
- /// </summary>
- [TestMethod]
- public void Credit_WithValidAmount_UpdatesBalance()
- {
- // Arrange
- var startBalance = 130.50;
- var endBalance = 145.54;
- var amount = 15.04;
- // Create account
- var account = new Account
- {
- Balance = startBalance,
- IsFrozen = false
- };
- // Create repository and add account
- var repository = new AccountRepository();
- repository.Add(account);
- // Change
- repository.Credit(account, amount);
- // Assert
- Assert.AreEqual(endBalance, account.Balance, 0.001, "Debit error");
- }
- /// <summary>
- /// Credit should throw the ArgumentOutOfRange exception, when amount is lower than 0.
- /// </summary>
- [TestMethod]
- [ExpectedException(typeof(ArgumentOutOfRangeException))]
- public void Credit_WithInvalidAmount_ShouldThrowArgumentOutOfRange()
- {
- // Arrange
- var startBalance = 20.00;
- var amount = -5.00;
- // Create account
- var account = new Account
- {
- Balance = startBalance,
- IsFrozen = false
- };
- // Create repository
- var repository = new AccountRepository();
- repository.Add(account);
- // Change
- repository.Credit(account, amount);
- }
- #endregion
- #region Transfer
- /// <summary>
- /// Transfer with valid amount should update balance.
- /// </summary>
- [TestMethod]
- public void Transfer_WithValidAmount_UpdatesBalance()
- {
- // Arrange
- var amount = 15.04;
- var fromStartBalance = 145.54;
- var fromEndBalance = 130.50;
- var toStartBalance = 20.00;
- var toEndBalance = 35.04;
- // Create accounts
- var fromAccount = new Account
- {
- Balance = fromStartBalance,
- IsFrozen = false
- };
- var toAccount = new Account
- {
- Balance = toStartBalance,
- IsFrozen = false
- };
- // Create repository and add account
- var repository = new AccountRepository();
- repository.Add(fromAccount);
- repository.Add(toAccount);
- // Change
- repository.Transfer(toAccount, fromAccount, amount);
- // Assert
- Assert.AreEqual(fromEndBalance, fromAccount.Balance, 0.001, "Debit error");
- Assert.AreEqual(toEndBalance, toAccount.Balance, 0.001, "Debit error");
- }
- /// <summary>
- /// Transfer should throw ArgumentOutOfRangeException
- /// exception when amount is 0 or under.
- /// </summary>
- [TestMethod]
- [ExpectedException(typeof(ArgumentOutOfRangeException))]
- public void Transfer_WithInValidAmount_ShouldThrowArgumentOutOfRange()
- {
- // Arrange
- var amount = -15.04;
- var fromStartBalance = 145.54;
- var toStartBalance = 20.00;
- // Create accounts
- var fromAccount = new Account
- {
- Balance = fromStartBalance,
- IsFrozen = false
- };
- var toAccount = new Account
- {
- Balance = toStartBalance,
- IsFrozen = false
- };
- // Create repository and add account
- var repository = new AccountRepository();
- repository.Add(fromAccount);
- repository.Add(toAccount);
- // Change
- repository.Transfer(toAccount, fromAccount, amount);
- }
- /// <summary>
- /// Transfer from an account with a balance too
- /// low should throw ArgumentOutOfRangeException exception.
- /// </summary>
- [TestMethod]
- [ExpectedException(typeof(ArgumentOutOfRangeException))]
- public void Transfer_WithLowBalance_ShouldThrowArgumentOutOfRange()
- {
- // Arrange
- var amount = 299.99;
- var fromStartBalance = 145.54;
- var toStartBalance = 20.00;
- // Create accounts
- var fromAccount = new Account
- {
- Balance = fromStartBalance,
- IsFrozen = false
- };
- var toAccount = new Account
- {
- Balance = toStartBalance,
- IsFrozen = false
- };
- // Create repository and add account
- var repository = new AccountRepository();
- repository.Add(fromAccount);
- repository.Add(toAccount);
- // Change
- repository.Transfer(toAccount, fromAccount, amount);
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement