Skip to content

Instantly share code, notes, and snippets.

@leandrosilva
Created September 14, 2012 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leandrosilva/3719247 to your computer and use it in GitHub Desktop.
Save leandrosilva/3719247 to your computer and use it in GitHub Desktop.
Mono + C# + NUnit
.DS_Store
*.dll
*.xml
#!/bin/sh
dmcs -t:library \
-r:nunit.framework.dll \
bank.cs \
bank_test.cs
#!/bin/sh
nunit-console bank.dll
namespace bank
{
using System;
public class Account
{
private float balance;
public void Deposit(float amount)
{
balance+=amount;
}
public void Withdraw(float amount)
{
balance-=amount;
}
public void TransferFunds(Account destination, float amount)
{
if (amount > balance)
{
throw new InvalidOperationException("Insufficient balance to transfer " + amount);
}
Withdraw(amount);
destination.Deposit(amount);
}
public float Balance
{
get { return balance; }
}
}
}
namespace bank
{
using System;
using NUnit.Framework;
[TestFixture]
public class AccountTest
{
[Test]
public void ShouldTransferFunds()
{
Account source = new Account();
source.Deposit(200.00F);
Account destination = new Account();
destination.Deposit(150.00F);
source.TransferFunds(destination, 100.00F);
Assert.AreEqual(250.00F, destination.Balance, "Destination account should have 250.00");
Assert.AreEqual(100.00F, source.Balance, "Source account should have 250.00");
}
[Test]
public void ShouldTransferFundsOnlyWhenHaveBalance()
{
Account source = new Account();
source.Deposit(200.00F);
Account destination = new Account();
destination.Deposit(150.00F);
source.TransferFunds(destination, 100.00F);
Assert.AreEqual(250.00F, destination.Balance, "Destination account should have 250.00");
Assert.IsInstanceOfType(typeof(InvalidOperationException), Eval(() => source.TransferFunds(destination, 300.00F)),
"Source have a insufficient balance to transfer 300.00");
}
public delegate void ThrowsDelegate();
private Exception Eval(ThrowsDelegate code)
{
try
{
code();
return null;
}
catch (Exception e)
{
return e;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment