Skip to content

Instantly share code, notes, and snippets.

Created July 16, 2010 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/478927 to your computer and use it in GitHub Desktop.
Save anonymous/478927 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'spec'
class Account
attr_reader :balance
def self.find(pin)
Accounts[pin]
end
def initialize(ref,balance=0)
@symbol = ref
@balance = balance
end
attr_reader :symbol
def ==(that)
@symbol == that.symbol
end
def withdraw_cash(amount)
@balance -= amount
amount
end
end
class ATM
def initialize
@num_attempts = 0
end
def read_pin(pin)
account = Account.find(pin)
if account.nil?
invalid_pin
else
return account
end
end
def invalid_pin
@num_attempts += 1
if @num_attempts >= 3
raise 'O NOES'
end
end
end
describe 'an atm' do
let (:atm) {ATM.new}
before(:each) do
Account::Accounts = {7643 => Account.new(:alices_account,2000), 5954 => Account.new(:bobes_account,22400), 1562 => Account.new(:clydes_account)}
end
describe 'entering a pin' do
context 'when a user enters a pin' do
it 'returns the account for alice\'s pin' do
atm.read_pin(7643).should == Account.new(:alices_account)
end
it 'returns the account for bobes pin' do
atm.read_pin(5954).should == Account.new(:bobes_account)
end
it 'returns the account for clyde\'s pin' do
atm.read_pin(1562).should == Account.new(:clydes_account)
end
it 'does not return the account for an invalid pin' do
atm.read_pin(:invalid_apin).should be_nil
end
it 'returns the same account twice' do
atm.read_pin(1562).should be_eql(atm.read_pin(1562))
end
it 'bobes account is not alices account' do
atm.read_pin(1562).should_not be_eql(atm.read_pin(5954))
end
end
context 'making three attempts to enter the pin' do
it 'fails after entering the wrong pin 3 times' do
2.times { atm.read_pin(:invalid_pin) }
lambda { atm.read_pin(:ohmygoshinvalidpin) }.should raise_error
end
end
end
describe 'withdrawing money' do
context 'from alices account' do
it 'gives us money' do
atm.read_pin(7643).withdraw_cash(80).should == 80
end
it 'debits the account' do
account = atm.read_pin(7643)
account.withdraw_cash(80)
account.balance.should == 2000 - 80
end
end
context 'from bobes account' do
it 'debits the account' do
account = atm.read_pin(5954)
account.withdraw_cash(250)
account.balance.should == 22400-250
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment