Back to Repositories

Testing Devise Helper Error Handling in heartcombo/devise

This test suite validates Devise’s helper functionality for handling user registration errors and internationalization (i18n) support. It specifically tests error message generation and display during the user registration process.

Test Coverage Overview

The test suite covers error handling scenarios in Devise’s registration process with both single and multiple validation errors. Key functionality includes:

  • Single error message generation and display
  • Multiple error message handling
  • i18n translation integration
  • Error message interpolation with resource names

Implementation Analysis

The testing approach utilizes Devise’s IntegrationTest framework with setup and teardown hooks for i18n configuration. The tests implement form submission scenarios with invalid inputs to trigger validation errors and verify proper error message display.

Pattern highlights include translation setup, form interaction simulation, and assertion checks for error message containers and content.

Technical Details

  • Testing Framework: Minitest
  • Integration Points: I18n backend
  • Setup Requirements: Translation configuration
  • Test Helpers: fill_in, click_button, assert_have_selector
  • Compatibility: Special handling for Mongoid versions

Best Practices Demonstrated

The test suite demonstrates several testing best practices including proper test isolation through setup/teardown hooks, clear test naming conventions, and comprehensive validation scenarios. It effectively tests both happy and error paths while maintaining proper separation of concerns between i18n configuration and test execution.

Notable practices include handling framework-specific edge cases (Mongoid version checks) and thorough validation of UI elements and content.

heartcombo/devise

test/helpers/devise_helper_test.rb

            
# frozen_string_literal: true

require 'test_helper'

class DeviseHelperTest < Devise::IntegrationTest
  setup do
    model_labels = { models: { user: "the user" } }
    translations = {
      errors: { messages: { not_saved: {
        one: "Can't save %{resource} because of 1 error",
        other: "Can't save %{resource} because of %{count} errors",
      } } },
      activerecord: model_labels,
      mongoid: model_labels
    }

    I18n.available_locales
    I18n.backend.store_translations(:en, translations)
  end

  teardown do
    I18n.reload!
  end

  test 'test errors.messages.not_saved with single error from i18n' do
    get new_user_registration_path

    fill_in 'password', with: 'new_user123'
    fill_in 'password confirmation', with: 'new_user123'
    click_button 'Sign up'

    assert_have_selector '#error_explanation'
    assert_contain "Can't save the user because of 1 error"
  end

  test 'test errors.messages.not_saved with multiple errors from i18n' do
    # Dirty tracking behavior prevents email validations from being applied:
    #    https://github.com/mongoid/mongoid/issues/756
    (pending "Fails on Mongoid < 2.1"; break) if defined?(Mongoid) && Mongoid::VERSION.to_f < 2.1

    get new_user_registration_path

    fill_in 'email', with: 'invalid_email'
    fill_in 'password', with: 'new_user123'
    fill_in 'password confirmation', with: 'new_user321'
    click_button 'Sign up'

    assert_have_selector '#error_explanation'
    assert_contain "Can't save the user because of 2 errors"
  end
end