Back to Repositories

Testing Vehicle Account Management Integration in Maybe Finance

This test suite validates the functionality of the Vehicles Controller in a Ruby on Rails application, focusing on vehicle account creation and updates. It ensures proper handling of vehicle-specific attributes and associated financial records within the Maybe Finance system.

Test Coverage Overview

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

  • Vehicle creation with associated account records
  • Vehicle updates with detailed attribute validation
  • Associated financial records creation (valuations and entries)
  • Currency and balance handling

Implementation Analysis

The testing approach utilizes Rails’ ActionDispatch::IntegrationTest framework with Minitest assertions. It implements the AccountableResourceInterfaceTest module for shared behavior testing and employs fixtures for test data setup. The tests verify both the persistence of vehicle-specific attributes and proper account relationship management.

Technical Details

  • Testing Framework: Minitest
  • Authentication: Custom sign_in helper
  • Fixtures: User and Account models
  • Assert Helpers: difference/no_difference for record count validation
  • Job Queue Testing: assert_enqueued_with for SyncJob verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper setup isolation, comprehensive state verification, and thorough edge case coverage. It validates both direct attributes and side effects of operations, ensures proper flash message setting, and verifies background job scheduling.

  • Isolated test setup using fixtures
  • Comprehensive state verification
  • Background job scheduling validation
  • Clear test organization and naming

maybe-finance/maybe

test/controllers/vehicles_controller_test.rb

            
require "test_helper"

class VehiclesControllerTest < ActionDispatch::IntegrationTest
  include AccountableResourceInterfaceTest

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

  test "creates with vehicle details" do
    assert_difference -> { Account.count } => 1,
      -> { Vehicle.count } => 1,
      -> { Account::Valuation.count } => 2,
      -> { Account::Entry.count } => 2 do
      post vehicles_path, params: {
        account: {
          name: "Vehicle",
          balance: 30000,
          currency: "USD",
          accountable_type: "Vehicle",
          accountable_attributes: {
            make: "Toyota",
            model: "Camry",
            year: 2020,
            mileage_value: 15000,
            mileage_unit: "mi"
          }
        }
      }
    end

    created_account = Account.order(:created_at).last

    assert_equal "Toyota", created_account.accountable.make
    assert_equal "Camry", created_account.accountable.model
    assert_equal 2020, created_account.accountable.year
    assert_equal 15000, created_account.accountable.mileage_value
    assert_equal "mi", created_account.accountable.mileage_unit

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

  test "updates with vehicle details" do
    assert_no_difference [ "Account.count", "Vehicle.count" ] do
      patch account_path(@account), params: {
        account: {
          name: "Updated Vehicle",
          balance: 28000,
          currency: "USD",
          accountable_type: "Vehicle",
          accountable_attributes: {
            id: @account.accountable_id,
            make: "Honda",
            model: "Accord",
            year: 2021,
            mileage_value: 20000,
            mileage_unit: "mi",
            purchase_price: 32000
          }
        }
      }
    end

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