Back to Repositories

Testing Transaction Import Controller Implementation in maybe-finance

This test suite validates the functionality of the imports controller in a Ruby on Rails application, focusing on transaction import operations. It ensures proper handling of CRUD operations, user authentication, and background processing for imports.

Test Coverage Overview

The test suite provides comprehensive coverage of the ImportsController functionality:

  • Index page rendering and import listing verification
  • New import form creation with Turbo Frame integration
  • Import creation with transaction type specification
  • Background processing of imports
  • Import deletion with proper cleanup

Implementation Analysis

The testing approach utilizes ActionDispatch::IntegrationTest for end-to-end verification of controller actions. It implements user authentication in setup, uses dom_id helpers for element selection, and leverages Turbo Frames for modal interactions.

The suite employs mock expectations for background job testing and validates flash messages and redirects.

Technical Details

  • Testing Framework: Minitest
  • Authentication: Custom sign_in helper
  • Fixtures: Used for user and import data
  • Mocking: Mocha for background job verification
  • DOM Testing: Built-in Rails assert_select
  • Response Validation: Standard Rails assertions

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Rails applications:

  • Proper setup and teardown management
  • Isolation of test cases
  • Verification of both happy path and edge cases
  • Use of fixtures for test data
  • Appropriate assertion selection for different scenarios
  • Clean separation of concerns in test organization

maybe-finance/maybe

test/controllers/imports_controller_test.rb

            
require "test_helper"

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

  test "gets index" do
    get imports_url

    assert_response :success

    @user.family.imports.ordered.each do |import|
      assert_select "#" + dom_id(import), count: 1
    end
  end

  test "gets new" do
    get new_import_url

    assert_response :success

    assert_select "turbo-frame#modal"
  end

  test "creates import" do
    assert_difference "Import.count", 1 do
      post imports_url, params: {
        import: {
          type: "TransactionImport"
        }
      }
    end

    assert_redirected_to import_upload_url(Import.all.ordered.first)
  end

  test "publishes import" do
    import = imports(:transaction)

    TransactionImport.any_instance.expects(:publish_later).once

    post publish_import_url(import)

    assert_equal "Your import has started in the background.", flash[:notice]
    assert_redirected_to import_path(import)
  end

  test "destroys import" do
    import = imports(:transaction)

    assert_difference "Import.count", -1 do
      delete import_url(import)
    end

    assert_redirected_to imports_path
  end
end