I'll copy and paste the different files and explain each one below, then I'll ask some questions that are starting to pile up.
So I practiced making a more complicated test than ever before on a bank account example. I made several files, a bank_account file and the spec and the "spec helper" which seems only to 'require relative' aka hook up files between each other. I do wonder exactly what a spec helper is, I read that it's meant to hold dependancies that would otherwise slow your other files down still not exactly sure what else it would contain but I guess in longer code specs would need some other things.
spec helper:
require 'rspec'
require_relative "../lib/bank_account"
I also made a 'bank account spec' file that holds the tests for my bank account. I made each test BEFORE making the method in the following file. This is the first time I've practiced test driven development. Let me post the file and I have questions about it below.
require "spec_helper"
RSpec.describe BankAccount do
let(:bank_account) {bank_account = BankAccount.new(5406327, 1000.00)}
describe ".new" do
it "takes an account number and a deposit amount as arguments" do
bank_account = BankAccount.new(5406327, 1000.00)
expect(bank_account).to be_a(BankAccount)
end
end
describe "#account_number" do
it "returns the account number" do
bank_account = BankAccount.new(5406327, 1000.00)
expect(bank_account.account_number).to eq(5406327)
end
describe "#initial_deposit" do
it "returns the initial deposit" do
bank_account = BankAccount.new(5406327, 1000.00)
expect(bank_account.initial_deposit). to eq(1000)
end
describe "#add_transaction" do
it "stores the transaction in an array" do
bank_account = BankAccount.new(5406327, 1000.00)
bank_account.add_transaction(-5.49)
expect(bank_account.transactions).to include(-5.49)
end
describe "#current balance" do
it "calculates the current balance" do
bank_account = BankAccount.new(5406327, 1000.00)
bank_account.add_transaction(-5)
bank_account.add_transaction(-10)
bank_account.add_transaction(-20)
expect(bank_account.current_balance).to eq(965)
end
end
describe "#summary" do
it "returns a text summary of the account" do
bank_account.add_transaction(-5)
bank_account.add_transaction(-10)
bank_account.add_transaction(-20)
expect(bank_account.summary).to include("Account Number: 5406327")
expect(bank_account.summary).to include ("initial deposit: $1000.00")
expect(bank_account.summary).to include("$-5.00")
end
end
end
end
end
end
RSpec.describe BankAccount do
let(:bank_account) {bank_account = BankAccount.new(5406327, 1000.00)}
describe ".new" do
it "takes an account number and a deposit amount as arguments" do
bank_account = BankAccount.new(5406327, 1000.00)
expect(bank_account).to be_a(BankAccount)
end
end
...what the heck is this 'let' business doing, I didin't have it before writing most of these tests and 5 of 6 of the methods in the file I'll post next, and my tests ran fine, then I got to the 6th test and suddenly it wasn't working without this 'let'.
also, my next question from the same file:
describe "#summary" do
it "returns a text summary of the account" do
bank_account.add_transaction(-5)
bank_account.add_transaction(-10)
bank_account.add_transaction(-20)
expect(bank_account.summary).to include("Account Number: 5406327")
expect(bank_account.summary).to include ("initial deposit: $1000.00")
expect(bank_account.summary).to include("$-5.00")
end
end
...by the time I've hit this, I've said 'expect.. to include'.. over and over again, seems repetitive or thats just how its done?
Also, you have "Account Number: blah blah... do I need the colons?
Anyway, the actual file with the methods that go with these tests is here:
class BankAccount
attr_accessor :account_number, :initial_deposit, :transactions
def initialize(account_number, initial_deposit)
@account_number = account_number
@initial_deposit = initial_deposit
@transactions = []
end
def add_transaction(amount)
@transactions << amount
end
def current_balance
result = initial_deposit
@transactions.each do |transaction|
result += transaction
end
return result
end
def summary
result = "Account Number: #{account_number}\n"
result += "Initial deposit: $"
end
end
I have so many questions about this. in the accessor, how come none of those things have their own method? where are they going and where are they defined??
ok well I guess thats really it. Here's my output- all but the last test is passing but here it is for now:
rspec
.....F
Failures:
1) BankAccount#account_number #initial_deposit #add_transaction #summary returns a text summary of the account
Failure/Error: expect(bank_account.summary).to include ("initial deposit: $1000.00")
expected "Account Number: 5406327\nInitial deposit: $" to include "initial deposit: $1000.00"
Diff:
@@ -1,2 +1,3 @@
-initial deposit: $1000.00
+Account Number: 5406327
+Initial deposit: $
# ./spec/lib/bank_account_spec.rb:46:in `block (6 levels) in <top (required)>'
Finished in 0.03309 seconds (files took 0.24607 seconds to load)
6 examples, 1 failure
Failed examples:
rspec ./spec/lib/bank_account_spec.rb:41 # BankAccount#account_number #initial_deposit #add_transaction #summary returns a text summary of the account
➜ methodTesting2 git:(master) ✗
...WHAT are the @ symbols telling me right now? I know this is passing, its green, I'm just not exactly sure how to read it. Need to say it out loud...read it like a book for practice. not sure what it is telling me, i could use to my advantage yet.
I'm also very confused about the difference between 'initial deposit', 'initial_deposit' and 'Initial Deposit'. Like yes I know they are slightly different but I'm not sure what needs to line up and how and why. At one point I think initial deposit needed to be capitalized and it was throwing an error.