Back to Repositories

Testing Category Management Controller Operations in maybe-finance

This test suite validates the Categories Controller functionality in a Ruby on Rails application using Minitest. It covers CRUD operations for financial transaction categories, including creation, updates, and bootstrapping default categories. The tests ensure proper category management with unique naming constraints and color assignments.

Test Coverage Overview

The test suite provides comprehensive coverage of category management operations including:

  • Basic CRUD operations (index, new, create, edit, update)
  • Validation of unique category names
  • Category creation with transaction assignment
  • Bulk category bootstrapping
  • Color management and validation

Implementation Analysis

The testing approach utilizes ActionDispatch::IntegrationTest for end-to-end controller testing. It employs Rails fixtures for test data setup and implements assertion-based validation patterns. The tests leverage Minitest’s built-in assertions and custom lambda-based change tracking for state verification.

Technical Details

  • Testing Framework: Minitest
  • Test Type: Integration Tests
  • Key Components: ActionDispatch, Fixtures
  • Authentication: Custom sign_in helper
  • Data Management: Database transactions and fixtures

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper setup and teardown management, isolated test cases, and comprehensive edge case coverage. It demonstrates effective use of fixtures, proper HTTP assertion checking, and thorough state validation. The code organization follows Rails convention with clear test case separation and meaningful assertions.

maybe-finance/maybe

test/controllers/categories_controller_test.rb

            
require "test_helper"

class CategoriesControllerTest < ActionDispatch::IntegrationTest
  setup do
    sign_in users(:family_admin)
    @transaction = account_transactions :one
  end

  test "index" do
    get categories_url
    assert_response :success
  end

  test "new" do
    get new_category_url
    assert_response :success
  end

  test "create" do
    color = Category::COLORS.sample

    assert_difference "Category.count", +1 do
      post categories_url, params: {
        category: {
          name: "New Category",
          color: color } }
    end

    new_category = Category.order(:created_at).last

    assert_redirected_to categories_url
    assert_equal "New Category", new_category.name
    assert_equal color, new_category.color
  end

  test "create fails if name is not unique" do
    assert_no_difference "Category.count" do
      post categories_url, params: {
        category: {
          name: categories(:food_and_drink).name,
          color: Category::COLORS.sample } }
    end

    assert_response :unprocessable_entity
  end

  test "create and assign to transaction" do
    color = Category::COLORS.sample

    assert_difference "Category.count", +1 do
      post categories_url, params: {
        transaction_id: @transaction.id,
        category: {
          name: "New Category",
          color: color } }
    end

    new_category = Category.order(:created_at).last

    assert_redirected_to categories_url
    assert_equal "New Category", new_category.name
    assert_equal color, new_category.color
    assert_equal @transaction.reload.category, new_category
  end

  test "edit" do
    get edit_category_url(categories(:food_and_drink))
    assert_response :success
  end

  test "update" do
    new_color = Category::COLORS.without(categories(:income).color).sample

    assert_changes -> { categories(:income).name }, to: "New Name" do
      assert_changes -> { categories(:income).reload.color }, to: new_color do
        patch category_url(categories(:income)), params: {
          category: {
            name: "New Name",
            color: new_color } }
      end
    end

    assert_redirected_to categories_url
  end

  test "bootstrap" do
    assert_difference "Category.count", 16 do
      post bootstrap_categories_url
    end

    assert_redirected_to categories_url
  end
end