title: Using Rspec, how can I test a method which calls a method from another class in ruby
body: <p>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!</p>
  <pre><code>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 &lt;top (required)&gt;'
  
  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      |
  +----------+--------------------+-------+--------+---------+
  </code></pre>
  <p>My Transaction class</p>
  <pre><code>class Transaction
    attr_reader :amount, :account, :timestamp
  
    def initialize(amount, account)
      @amount = amount
      @account = account
      @timestamp = Time.now.strftime(&quot;%d/%m/%Y&quot;)
    end
  
    def balance
      @account.balance_after(self)
    end
  
    def formate
      @amount &gt; 0 ? (puts &quot;#{@timestamp}|| #{@amount} ||  ||  #{balance}&quot;)
      : (puts &quot;#{@timestamp}||  || #{@amount.abs}  ||  #{balance}&quot;)
    end
  end
  </code></pre>
  <p>My Account class</p>
  <pre><code>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 &lt;&lt; Transaction.new(amount, self)
    end
  
    def withdraw(amount)
      @balance -= amount
      @transactions &lt;&lt; Transaction.new(-amount, self)
    end
  
    def balance_after(transaction)
      index = @transactions.find_index(transaction)
      @transactions[0..index].map(&amp;:amount).sum
    end
  
    def print_statement
      puts HEADER
      @transactions.reverse.map(&amp;:formate)
    end
  end
  </code></pre>
  <p>My Transaction spec</p>
  <pre><code>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
  </code></pre>