byronmac
byronmac

Reputation: 27

Using Rspec, how can I test a method which calls a method from another class in ruby

My Transaction class has a method 'balance' which calls a method 'balance_after' from my Account class. I would like to make a test where I can check the 'balance_after' method is being called with the 'balance' method. I am new to both ruby and testing, so I appreciate any guidance! I am using simple-cov to get my coverage and i'd like to hit 100% if possible. I hope I have provided enough information. Thanks in advance!

Failures:

  1) Transaction#balance checks balance
     Failure/Error: @account.balance_after(self)
     
     NoMethodError:
       undefined method `balance_after' for :account:Symbol
     # ./lib/transaction.rb:11:in `balance'
     # ./spec/transaction_spec.rb:19:in `block (3 levels) in <top (required)>'

Finished in 0.04089 seconds (files took 0.87223 seconds to load)
10 examples, 1 failure

Failed examples:

rspec ./spec/transaction_spec.rb:18 # Transaction#balance checks balance


COVERAGE:  98.55% -- 68/69 lines in 4 files

+----------+--------------------+-------+--------+---------+
| coverage | file               | lines | missed | missing |
+----------+--------------------+-------+--------+---------+
|  90.91%  | lib/transaction.rb | 11    | 1      | 16      |
+----------+--------------------+-------+--------+---------+

My Transaction class

class Transaction
  attr_reader :amount, :account, :timestamp

  def initialize(amount, account)
    @amount = amount
    @account = account
    @timestamp = Time.now.strftime("%d/%m/%Y")
  end

  def balance
    @account.balance_after(self)
  end

  def formate
    @amount > 0 ? (puts "#{@timestamp}|| #{@amount} ||  ||  #{balance}")
    : (puts "#{@timestamp}||  || #{@amount.abs}  ||  #{balance}")
  end
end

My Account class

require_relative 'transaction'

class Account
  attr_reader :balance, :transactions

  HEADER = 'date || credit || debit || balance'

  def initialize
    @balance = 0
    @transactions = []
  end

  def deposit(amount)
    @balance += amount
    @transactions << Transaction.new(amount, self)
  end

  def withdraw(amount)
    @balance -= amount
    @transactions << Transaction.new(-amount, self)
  end

  def balance_after(transaction)
    index = @transactions.find_index(transaction)
    @transactions[0..index].map(&:amount).sum
  end

  def print_statement
    puts HEADER
    @transactions.reverse.map(&:formate)
  end
end

My Transaction spec

require 'transaction'

describe Transaction do
  let(:transaction) { Transaction.new(:amount, :account) }
  let(:timestamp) { Time.now.strftime('%d/%m/%Y') }

  describe '#initilalize' do
    it 'validates class' do
      expect(transaction).to be_a Transaction
    end

    it 'stores dates' do
      expect(transaction.timestamp).to eq timestamp
    end
  end

  describe '#balance' do
    it 'checks balance' do
      expect(transaction.balance).to eq true
    end
  end
end

Upvotes: 0

Views: 1172

Answers (1)

Schwern
Schwern

Reputation: 165576

You have to call the method correctly. Transaction.new takes an amount, presumably a number, and some sort of Account object which has balance_after defined. You've given it two Symbols.

describe Transaction do
  let(:account) { Account.new }
  let(:amount) { 1.23 }
  let(:transaction) { Transaction.new(amount, account) }
  let(:timestamp) { Time.now.strftime('%d/%m/%Y') }

Then check that it returns what you expect. balance does not return true or false, it returns a balance. Specifically, it's just a pass through to account.balance_after(transaction), so test that.

  describe '#balance' do
    it 'checks balance' do
      expect(transaction.balance).to eq account.balance_after(transaction)
    end
  end

This might seem circular, but this is just an integration test. account.balance_after would be unit tested as part of Account's tests. There's no need to repeat those tests here.

Upvotes: 1

Related Questions