Created
April 11, 2012 16:49
-
-
Save benfoster/2360471 to your computer and use it in GitHub Desktop.
BDD with MSpec
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 BDDDemo | |
{ | |
//Story: Account Holder withdraws cash | |
//As an Account Holder | |
//I want to withdraw cash from an ATM | |
//So that I can get money when the bank is closed | |
//Scenario 1: Account has sufficient funds | |
//Given the account balance is \$100 | |
// And the card is valid | |
// And the machine contains enough money | |
//When the Account Holder requests \$20 | |
//Then the ATM should dispense \$20 | |
// And the account balance should be \$80 | |
// And the card should be returned | |
//Scenario 2: Account has insufficient funds | |
//Given the account balance is \$10 | |
// And the card is valid | |
// And the machine contains enough money | |
//When the Account Holder requests \$20 | |
//Then the ATM should not dispense any money | |
// And the ATM should say there are insufficient funds | |
// And the account balance should be \$20 | |
// And the card should be returned | |
//Scenario 3: Card has been disabled | |
//Given the card is disabled | |
//When the Account Holder requests \$20 | |
//Then the ATM should retain the card | |
//And the ATM should say the card has been retained | |
public class ATMSpecs | |
{ | |
[Subject("Account Holder withdraws cash")] | |
public class Account_holder_withdraws_cash | |
{ | |
public class When_the_account_has_sufficient_funds_and_the_card_is_valid | |
{ | |
Establish ctx = () => | |
{ | |
account = new Account(100); | |
atm = new ATM(account); | |
}; | |
Because of = () => amountWithdrawn = atm.WithdrawCash(20); | |
It Should_dispense_the_requested_amount = () => amountWithdrawn.ShouldEqual(20); | |
It Should_debit_the_account_by_the_requested_amount = () => account.balance.ShouldEqual(80); | |
It Should_return_the_card; | |
static ATM atm; | |
static Account account; | |
static decimal amountWithdrawn; | |
} | |
public class When_the_account_has_insufficient_funds_and_the_card_is_valid | |
{ | |
It Should_not_dispense_any_money; | |
It Should_not_alter_the_account_balance; | |
It Should_return_the_card; | |
} | |
public class When_the_card_has_been_disabled | |
{ | |
It Should_retain_the_card; | |
} | |
} | |
} | |
public class ATM | |
{ | |
private readonly Account account; | |
public ATM(Account account) | |
{ | |
this.account = account; | |
} | |
public decimal WithdrawCash(decimal amount) | |
{ | |
if (account.TryDebit(amount)) | |
return amount; | |
return decimal.Zero; | |
} | |
} | |
public class Account | |
{ | |
public decimal balance; | |
public Account(decimal openingBalance) | |
{ | |
balance = openingBalance; | |
} | |
public bool TryDebit(decimal amount) | |
{ | |
if (HasSufficientFunds(amount)) | |
{ | |
balance -= amount; | |
return true; | |
} | |
return false; | |
} | |
protected bool HasSufficientFunds(decimal amount) | |
{ | |
return balance > amount; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output:
Account Holder withdraws cash, When the account has sufficient funds and the card is valid
¯ Should dispense the requested amount
¯ Should debit the account by the requested amount
¯ Should return the card (NOT IMPLEMENTED)
Account Holder withdraws cash, When the account has insufficient funds and the card is valid
¯ Should not dispense any money (NOT IMPLEMENTED)
¯ Should not alter the account balance (NOT IMPLEMENTED)
¯ Should return the card (NOT IMPLEMENTED)
Account Holder withdraws cash, When the card has been disabled
¯ Should retain the card (NOT IMPLEMENTED)