Created
September 14, 2012 01:25
-
-
Save leandrosilva/3719247 to your computer and use it in GitHub Desktop.
Mono + C# + NUnit
This file contains 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
.DS_Store | |
*.dll | |
*.xml |
This file contains 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
#!/bin/sh | |
dmcs -t:library \ | |
-r:nunit.framework.dll \ | |
bank.cs \ | |
bank_test.cs |
This file contains 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
#!/bin/sh | |
nunit-console bank.dll |
This file contains 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
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; } | |
} | |
} | |
} |
This file contains 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
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