I have a model name Loan
. Each loan has JournalEntry
and each JournalEntry
has many JournalEntryLine
. Also, each Loan
has many Charge
and each Charge
has one JournalEntry
with many JournalEntryLine
. Same with Payment
. So, for my app is necessary to check if the balance of each JournalEntryLine
sums zero. So, Loan
model has a method to print in the console each JournalEntryLine
with their respective Account
. And then sum them all and it should return zero. In Rails console, works perfectly. But in RSpec, doesn't. I'm guessing that RSpec lazy load the objects, so doesn't load them with the method. But, when I bring up the JournalEntryLine
instances, are shown correctly. My problem is not what is shown in the RSpec console, but the summation always returns 0.00
, so I can't test if the JournalEntryLines are generated correctly.
This is the test:
RSpec.describe 'Normal cancellation', type: :feature do
before :each do
prepare_db_for_loan
prepare_loan_and_payments
log_in
end
describe 'In a 12 months loan, with fortnight periodicity, in it last payment.' do
it 'Should have a current balance equals to B/. 59.25' do
expect(@loan.current_balance).to eq 59.25.to_d ## This method calls all charges and sum its balance value.
expect(@loan.accounting_closing).to eq 0.00.to_d ## This is the method that sums JournalEntryLines.
expect(@loan.status).to eq 'vigente'
end
it 'Should have a current balance equals to B/. 1.00 and be still "vigente"' do
create_payment(@loan, '2021-01-15', (@loan.payment_amt - 1))
expect(@loan.current_balance).to eq(1)
end
it 'Shuold have a current balance equals to B/. 0.00 and be cancelled' do
create_payment(@loan, '2021-01-15', (@loan.payment_amt - 1))
expect(@loan.current_balance).to eq(1)
end
end
end
This is Loan#accounting_closing
def accounting_closing
## get accounts
@accounts = Account.all.map do |account|
{
'number' => account.account_number,
'name' => account.account_name,
'debit' => 0,
'credit' => 0
}
end
## get JournalEntryLines from loan.journal_entry
@lines = journal_entry.lines.map { |line| line }
## get JournalEntryLines from loan.charges
charges.where.not(charge_type: 'Annulled').each do |charge|
@lines += charge.journal_entry.lines.map { |line| line }
end
## get JournalEntryLines from loan.payments
payments.each do |payment|
@lines += payment.journal_entry.lines.map { |line| line }
end
## make hashes
@lines.each do |line|
@accounts.each do |account|
if account['number'] == line.account_number
account['debit'] += line.debit
account['credit'] += line.credit
end
end
end
@accounts = @accounts.reject do |account|
account['debit'] == 0 && account['credit'] == 0
end
## show @accounts hash
@accounts.each do |account|
account['debit'] = account['debit'].to_d.round 2
account['credit'] = account['credit'].to_d.round 2
account['balance'] = account['debit'].to_d.round(2) - account['credit'].to_d.round(2)
end
p '*-*-' * 20
p ' # | debit | credit | blanc | name'
@accounts.each do |account|
p "#{account['number']} | #{account['debit'].to_d.round 2} | #{account['credit'].to_d.round 2} | #{account['balance'].to_d.round 2} | #{account['name']}"
end
p ' === ' * 20
@debit = @accounts.map { |account| account['debit'] }.sum.to_d.round 2
@credit = @accounts.map { |account| account['credit'] }.sum.to_d.round 2
@balance = @accounts.map { |account| account['balance'] }.sum.to_d.round 2
p "Total: | #{@debit} | #{@credit} | #{@balance} "
p '*-*-' * 20
@balance
end
And this is Loan#current_balance
:
def current_balance
charges.where('balance <> 0').sum(:balance)
end
Aucun commentaire:
Enregistrer un commentaire