Last active
March 12, 2018 19:46
-
-
Save jsheridanwells/e332d054ef253ff97263cbb8e7676bad to your computer and use it in GitHub Desktop.
Basic C# Unit Tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using BankAccountNS; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace BankTest | |
{ | |
[TestClass] // Tests are set in test classes... | |
public class BankAccountTests | |
{ | |
[TestMethod] // ... and test methods | |
public void Debit_WithValidAmount_UpdatesBalance() | |
{ | |
double beginningBalance = 11.99; | |
double debitAmount = 4.55; | |
double expected = 7.44; | |
BankAccount account = new BankAccount("Mr. Jeremy Wells", beginningBalance); // set up the class to test | |
account.Debit(debitAmount); | |
double actual = account.Balance; | |
Assert.AreEqual(expected, actual, 0.001, "Account not debited correctly"); // assert equal, also indicate precision and message | |
} | |
[TestMethod] | |
public void Debit_WhenAmountLessThanZero_ShouldThrowArgumentOutOfRange() | |
{ | |
double beginningBalance = 11.999; | |
double debitAmount = -100; | |
BankAccount account = new BankAccount("Mr. Jeremy Wells", beginningBalance); | |
try | |
{ | |
account.Debit(debitAmount); | |
} | |
catch (ArgumentOutOfRangeException e) | |
{ | |
StringAssert.Contains(e.Message, BankAccount.DebitAmountLessThanZeroMessage); // make sure exception message matches specified message | |
} | |
} | |
[TestMethod] | |
public void Debit_WithAmountExceedingBalance_ShouldThrowArgumentOutOfRange() | |
{ | |
double beginningBalance = 11.99; | |
double debitAmount = 100; | |
BankAccount account = new BankAccount("Mr. Jeremy Wells", beginningBalance); | |
try | |
{ | |
account.Debit(debitAmount); | |
} | |
catch (ArgumentOutOfRangeException e) | |
{ | |
StringAssert.Contains(e.Message, BankAccount.DebitAmountExceedsBalanceMessage); | |
return; // exit method if exception is thrown | |
} | |
Assert.Fail("No exception was thrown"); // fail if no exception is thrown | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment