Skip to content

Instantly share code, notes, and snippets.

@waldothedeveloper
Last active July 17, 2017 19:12
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 waldothedeveloper/75d950b99b15c8f404d88fd79ebc91c1 to your computer and use it in GitHub Desktop.
Save waldothedeveloper/75d950b99b15c8f404d88fd79ebc91c1 to your computer and use it in GitHub Desktop.
Bank Account HM
class BankAccount
attr_reader :balance
@@minimum_balance = 200
def initialize(opening_balance, account_holder)
raise ArgumentError if opening_balance < @@minimum_balance
@balance = opening_balance
end
def deposit amount
@balance += amount
end
def withdraw amount
@balance -= amount
end
def transfer amount, account
withdraw amount
account.deposit amount
end
def self.minimum_balance= amount
@@minimum_balance = amount
end
end
require './bank_account.rb'
describe BankAccount do
context "has a balance" do
let(:account) do
account = BankAccount.new(500, "Sarah")
end
it "is created with an opening balance and the name of the client" do
expect(account).to be_a(BankAccount)
end
it "can report it's balance" do
expect(account.balance).to eq(500)
end
end
context "making a deposit" do
let(:account) do
account = BankAccount.new(500, "Sarah")
account.deposit(500)
account
end
it "balance is increased" do
expect(account.balance).to eq(1000)
end
end
context "making a withdrawal" do
let(:account) do
account = BankAccount.new(500, "Sarah")
account.withdraw(200)
account
end
it "balance is decreased" do
expect(account.balance).to eq(300)
end
end
context "transfering funds" do
let(:account) do
account = BankAccount.new(500, "Sarah")
end
let(:other_account) do
other_account = BankAccount.new(1000, "Todd")
end
before :each do
account.transfer(200, other_account)
end
it "account balance is decreased" do
expect(account.balance).to eq(300)
end
it "other account balance is increased" do
expect(other_account.balance).to eq(1200)
end
end
context "minimum balance" do
it "raises an error if opening balance is too low" do
expect{ BankAccount.new(199, "Terry") }.to raise_error(ArgumentError)
end
it "does NOT raise an error if opening balance is over minimum balance" do
expect{ BankAccount.new(201, "Terry") }.not_to raise_error
end
it "allows the bank owner to change the minimum balance" do
BankAccount.minimum_balance = 100
expect(BankAccount.minimum_balance).to eq(100)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment