Back to Repositories

Testing Property Management Controller Integration in Maybe Finance

This test suite validates the functionality of the Properties Controller in the Maybe Finance application, focusing on property account creation and updates with detailed address information and property specifications.

Test Coverage Overview

The test suite provides comprehensive coverage for property management operations.

Key areas tested include:
  • Property account creation with full details including address
  • Property updates with validation of associated records
  • Database record count verification across multiple models
  • Flash message confirmations
  • Background job enqueueing

Implementation Analysis

The implementation uses ActionDispatch::IntegrationTest for end-to-end testing of the properties controller. It leverages Minitest’s assertion syntax and includes the AccountableResourceInterfaceTest module for shared behavior testing.

Notable patterns include:
  • Setup blocks for user authentication and test data preparation
  • Lambda usage for multiple count assertions
  • Nested parameter structures for complex property data

Technical Details

Testing infrastructure includes:
  • Minitest as the testing framework
  • ActionDispatch for integration testing
  • Fixtures for test data management
  • Custom test helper methods
  • SyncJob background job testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Comprehensive setup and teardown management
  • Atomic test cases with clear assertions
  • Proper isolation of test scenarios
  • Validation of both successful operations and side effects
  • Testing of complex nested attributes
  • Verification of user feedback and system state

maybe-finance/maybe

test/controllers/properties_controller_test.rb

            
require "test_helper"

class PropertiesControllerTest < ActionDispatch::IntegrationTest
  include AccountableResourceInterfaceTest

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

  test "creates with property details" do
    assert_difference -> { Account.count } => 1,
      -> { Property.count } => 1,
      -> { Account::Valuation.count } => 2,
      -> { Account::Entry.count } => 2 do
      post properties_path, params: {
        account: {
          name: "Property",
          balance: 500000,
          currency: "USD",
          accountable_type: "Property",
          accountable_attributes: {
            year_built: 2002,
            area_value: 1000,
            area_unit: "sqft",
            address_attributes: {
              line1: "123 Main St",
              line2: "Apt 1",
              locality: "Los Angeles",
              region: "CA", # ISO3166-2 code
              country: "US", # ISO3166-1 Alpha-2 code
              postal_code: "90001"
            }
          }
        }
      }
    end

    created_account = Account.order(:created_at).last

    assert created_account.accountable.year_built.present?
    assert created_account.accountable.address.line1.present?

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

  test "updates with property details" do
    assert_no_difference [ "Account.count", "Property.count" ] do
      patch account_path(@account), params: {
        account: {
          name: "Updated Property",
          balance: 500000,
          currency: "USD",
          accountable_type: "Property",
          accountable_attributes: {
            id: @account.accountable_id,
            year_built: 2002,
            area_value: 1000,
            area_unit: "sqft",
            address_attributes: {
              line1: "123 Main St",
              line2: "Apt 1",
              locality: "Los Angeles",
              region: "CA", # ISO3166-2 code
              country: "US", # ISO3166-1 Alpha-2 code
              postal_code: "90001"
            }
          }
        }
      }
    end

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