title: How do I mock a class with initialized methods using Rspec?
body: <p>I am trying to practice using doubles and mocking in my Rspec tests in order to isolate my dependencies. At the moment I am making a simple banking program that has 2 classes - an Account class, responsible for the transaction logic such as updating balance, crediting, debiting etc. I also have a Statement class, responsible for formatting a bank statement based on the account transaction history. </p>
  
  <p>My Account class is initialized as follows: </p>
  
  <pre><code>def initialize(statement = Statement.new)
      @balance = 0.00
      @credit = ''
      @debit = ''
      @date = Date.today.strftime('%d/%m/%Y')
      @transaction = []
      @statement = statement
  end
  </code></pre>
  
  <p>My Statement class is iniitalized as follows: </p>
  
  <pre><code>  def initialize
      @display = []
    end
  </code></pre>
  
  <p>(The display array is an array of transaction arrays, which are all the transactions that have occurred in that particular account). </p>
  
  <p>In my statement_spec.rb file, I want to use a double of the Account class. How do I set up an Account double that already has a Statement with a full display? At the moment I have:</p>
  
  <p><code>account = double(Account.new(statement = statement))</code> but I want to make sure that the display has transactions in them so I can test the Statement class methods with an a fake, functioning account? </p>
  
  <p>Sorry if I haven't explained that very clearly. Struggling to get my head around mocking so any help is really appreciated! Thanks :) </p>
  
  <p>UPDATE: 
  The method I am testing is this:</p>
  
  <pre><code>  def format
      puts 'date || credit || debit || balance'
      @display.map do |transaction|
        return @display.join(' || ')
      end
    end
  </code></pre>
  
  <p>And the current test is this:</p>
  
  <pre><code>  describe '#format' do
      it 'prints the statement as a table' do
        account = Account.new
        account.deposit(15.00)
        account.complete_transaction
        expect(account.statement.format).to eq Date.today.strftime('%d/%m/%Y') + ' || 15.00 ||  || 15.00'
      end
    end
  </code></pre>