Back to Repositories

Testing Tag Management Controller Integration in Maybe Finance

This test suite validates the TagsController functionality in a Ruby on Rails application using Minitest. It covers essential CRUD operations for tag management, including listing, creating, editing, and updating tags within a family-based user context.

Test Coverage Overview

The test suite provides comprehensive coverage of the TagsController endpoints and actions.

  • Index action verification with DOM element checking
  • New tag form accessibility testing
  • Tag creation with count assertion
  • Edit form accessibility validation
  • Tag update functionality verification

Implementation Analysis

The implementation follows Rails integration testing patterns using ActionDispatch::IntegrationTest. The suite employs user authentication setup and family-scoped tag management, utilizing fixtures for test data. DOM testing is implemented through assert_select helpers.

Technical Details

  • Testing Framework: Minitest
  • Integration Layer: ActionDispatch
  • Authentication: Custom sign_in helper
  • Fixtures: User and tag data
  • DOM Testing: Rails assert_select
  • Flash Message Validation

Best Practices Demonstrated

The test suite demonstrates several testing best practices for Rails applications.

  • Proper test setup with authentication
  • Atomic test cases for each controller action
  • Assertion of both response codes and side effects
  • Flash message verification
  • DOM element presence validation
  • Database count assertions for mutations

maybe-finance/maybe

test/controllers/tags_controller_test.rb

            
require "test_helper"

class TagsControllerTest < ActionDispatch::IntegrationTest
  setup do
    sign_in @user = users(:family_admin)
  end

  test "should get index" do
    get tags_url
    assert_response :success

    @user.family.tags.each do |tag|
      assert_select "#" + dom_id(tag), count: 1
    end
  end

  test "should get new" do
    get new_tag_url
    assert_response :success
  end

  test "should create tag" do
    assert_difference("Tag.count") do
      post tags_url, params: { tag: { name: "Test Tag" } }
    end

    assert_redirected_to tags_url
    assert_equal "Tag created", flash[:notice]
  end

  test "should get edit" do
    get edit_tag_url(tags.first)
    assert_response :success
  end

  test "should update tag" do
    patch tag_url(tags.first), params: { tag: { name: "Test Tag" } }

    assert_redirected_to tags_url
    assert_equal "Tag updated", flash[:notice]
  end
end