Back to Repositories

Testing Account Valuation Controller Implementation in Maybe Finance

This test suite validates the functionality of account valuations in a financial application, focusing on CRUD operations and data integrity. It ensures proper handling of account entries, valuations, and associated synchronization processes.

Test Coverage Overview

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

  • Duplicate valuation prevention
  • Basic entry creation with validation
  • Entry update functionality
  • Synchronization job triggering
Integration points include user authentication, account management, and background job processing.

Implementation Analysis

The testing approach utilizes ActionDispatch::IntegrationTest with EntryableResourceInterfaceTest inclusion for shared behavior. The implementation leverages Rails’ testing patterns with fixtures for data setup and assertion helpers for response validation.

Key patterns include setup blocks for test initialization, difference assertions for database changes, and response status verification.

Technical Details

  • Testing Framework: Minitest
  • Fixtures: User and Account Entry data
  • Authentication: Custom sign_in helper
  • Response Assertions: Standard Rails assertions
  • Background Jobs: SyncJob verification

Best Practices Demonstrated

The test suite exemplifies strong testing practices through isolated test cases, proper setup/teardown management, and comprehensive edge case coverage. Notable practices include:

  • Atomic test cases with clear purpose
  • Proper use of fixtures and setup blocks
  • Validation of both happy path and error scenarios
  • Background job verification

maybe-finance/maybe

test/controllers/account/valuations_controller_test.rb

            
require "test_helper"

class Account::ValuationsControllerTest < ActionDispatch::IntegrationTest
  include EntryableResourceInterfaceTest

  setup do
    sign_in @user = users(:family_admin)
    @entry = account_entries(:valuation)
  end

  test "error when valuation already exists for date" do
    assert_no_difference [ "Account::Entry.count", "Account::Valuation.count" ] do
      post account_valuations_url(@entry.account), params: {
        account_entry: {
          amount: 19800,
          date: @entry.date,
          currency: "USD"
        }
      }
    end

    assert_response :unprocessable_entity
  end

  test "creates entry with basic attributes" do
    assert_difference [ "Account::Entry.count", "Account::Valuation.count" ], 1 do
      post account_valuations_url, params: {
        account_entry: {
          name: "New entry",
          amount: 10000,
          currency: "USD",
          date: Date.current,
          account_id: @entry.account_id
        }
      }
    end

    created_entry = Account::Entry.order(created_at: :desc).first

    assert_enqueued_with job: SyncJob

    assert_redirected_to account_url(created_entry.account)
  end

  test "updates entry with basic attributes" do
    assert_no_difference [ "Account::Entry.count", "Account::Valuation.count" ] do
      patch account_valuation_url(@entry), params: {
        account_entry: {
          name: "Updated entry",
          amount: 20000,
          currency: "USD",
          date: Date.current
        }
      }
    end

    assert_enqueued_with job: SyncJob

    assert_redirected_to account_url(@entry.account)
  end
end