mardi 10 novembre 2020

Ruby testing basic - trouble mocking a class

I am building an in terminal banking app using ruby and rspec. The application should be able to save each transaction for a user to display. I am currently attempting to mock this function.

Here is my RecordTransaction class

class TransactionRecord
  attr_reader :credit, :debit, :balance

  def initialize(credit: nil, debit: nil, balance: nil)
    @date = date
    @credit = credit
    @debit = debit
    @balance = balance
  end
end

and here is my transaction class where the above class is used.

class Transaction
  attr_reader :history, :transaction_record

  def initialize(transaction_record = TransactionRecord)
    @transaction_record = transaction_record
    @history = []
  end

  def credit(balance, amount)
    update_balance = balance + amount
    @history << @transaction_record.new(balance: update_balance, credit: amount)
  end
end 

how can I mock this in my transaction_spec?

here is what I currently have

describe Transaction do
  let(:transaction_record) { double(date: '2020-11-10', credit: 200, debit: nil, balance: 300) } //trying to mock the expected output.
  subject { described_class.new}

  describe '#initalize' do

    it 'is initialized with an empty history array' do
      expect(subject.history).to be_empty
    end
  end

  describe '#credit' do
    it 'credits the the amount according to the balance' do
      expect(subject.credit(100, 200)).to eq transaction_record
    end
  end

however the error I am getting is

 expected: #<Double (anonymous)>
            got: [#<TransactionRecord:0x00007fbe7710afd0 @credit=200, @debit=nil, @balance=300>]

Aucun commentaire:

Enregistrer un commentaire