Back to Repositories

Testing Loan Account Management Implementation in maybe-finance

This test suite validates the functionality of loan account management in a financial application, focusing on creation and update operations with detailed loan parameters. The tests ensure proper handling of loan-specific attributes and associated financial records.

Test Coverage Overview

The test suite provides comprehensive coverage of loan account operations, including:

  • Creation of loan accounts with associated financial records
  • Validation of loan-specific parameters (interest rate, term, rate type)
  • Account updates with modified loan details
  • Verification of associated records (valuations, entries)

Implementation Analysis

The testing approach utilizes ActionDispatch::IntegrationTest for end-to-end validation of loan controller actions. It implements user authentication setup and leverages Rails fixtures for test data. The tests verify both the persistence of loan details and proper HTTP responses.

Technical Details

  • Testing Framework: Minitest
  • Authentication: Custom sign_in helper
  • Fixtures: Users and accounts
  • Assertions: Database counts, attribute equality, redirects
  • Background Jobs: SyncJob verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices including atomic test cases, proper setup/teardown management, and comprehensive state verification. It effectively uses assertion helpers and demonstrates careful attention to database record counts and attribute validation.

maybe-finance/maybe

test/controllers/loans_controller_test.rb

            
require "test_helper"

class LoansControllerTest < ActionDispatch::IntegrationTest
  include AccountableResourceInterfaceTest

  setup do
    sign_in @user = users(:family_admin)
    @account = accounts(:loan)
  end

  test "creates with loan details" do
    assert_difference -> { Account.count } => 1,
      -> { Loan.count } => 1,
      -> { Account::Valuation.count } => 2,
      -> { Account::Entry.count } => 2 do
      post loans_path, params: {
        account: {
          name: "New Loan",
          balance: 50000,
          currency: "USD",
          accountable_type: "Loan",
          accountable_attributes: {
            interest_rate: 5.5,
            term_months: 60,
            rate_type: "fixed"
          }
        }
      }
    end

    created_account = Account.order(:created_at).last

    assert_equal "New Loan", created_account.name
    assert_equal 50000, created_account.balance
    assert_equal "USD", created_account.currency
    assert_equal 5.5, created_account.accountable.interest_rate
    assert_equal 60, created_account.accountable.term_months
    assert_equal "fixed", created_account.accountable.rate_type

    assert_redirected_to created_account
    assert_equal "Loan account created", flash[:notice]
    assert_enqueued_with(job: SyncJob)
  end

  test "updates with loan details" do
    assert_no_difference [ "Account.count", "Loan.count" ] do
      patch account_path(@account), params: {
        account: {
          name: "Updated Loan",
          balance: 45000,
          currency: "USD",
          accountable_type: "Loan",
          accountable_attributes: {
            id: @account.accountable_id,
            interest_rate: 4.5,
            term_months: 48,
            rate_type: "fixed"
          }
        }
      }
    end

    @account.reload

    assert_equal "Updated Loan", @account.name
    assert_equal 45000, @account.balance
    assert_equal 4.5, @account.accountable.interest_rate
    assert_equal 48, @account.accountable.term_months
    assert_equal "fixed", @account.accountable.rate_type

    assert_redirected_to @account
    assert_equal "Loan account updated", flash[:notice]
    assert_enqueued_with(job: SyncJob)
  end
end