Back to Repositories

Testing Devise Controller Generator Implementation in heartcombo/devise

This test suite validates the Devise controllers generator functionality, ensuring proper controller generation with different scopes and parameters. It verifies both the creation and non-creation of authentication-related controllers based on specified inputs.

Test Coverage Overview

The test suite provides comprehensive coverage of the Devise controllers generator:
  • Verifies no controller creation without parameters
  • Tests controller generation with different user scopes
  • Validates selective controller generation with specific scope parameters
  • Covers all authentication controller types including sessions, registrations, confirmations, passwords, unlocks, and OAuth callbacks

Implementation Analysis

The testing approach utilizes Rails::Generators::TestCase for generator testing:
  • Implements setup methods for temporary destination preparation
  • Uses generator runner with various parameter combinations
  • Employs file assertion methods to verify controller existence and content
  • Implements helper methods for repeated class name validations

Technical Details

Testing infrastructure includes:
  • Rails generator testing framework
  • File system assertions for controller verification
  • Custom assertion helpers for class name validation
  • Temporary directory setup for test isolation
  • Standard Ruby test helper integration

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolated test cases with clear, single-responsibility assertions
  • Proper test setup and teardown management
  • Comprehensive negative and positive test cases
  • DRY principle through helper method extraction
  • Clear test naming and organization

heartcombo/devise

test/generators/controllers_generator_test.rb

            
# frozen_string_literal: true

require "test_helper"

class ControllersGeneratorTest < Rails::Generators::TestCase
  tests Devise::Generators::ControllersGenerator
  destination File.expand_path("../../tmp", __FILE__)
  setup :prepare_destination

  test "Assert no controllers are created with no params" do
    capture(:stderr) { run_generator }
    assert_no_file "app/controllers/sessions_controller.rb"
    assert_no_file "app/controllers/registrations_controller.rb"
    assert_no_file "app/controllers/confirmations_controller.rb"
    assert_no_file "app/controllers/passwords_controller.rb"
    assert_no_file "app/controllers/unlocks_controller.rb"
    assert_no_file "app/controllers/omniauth_callbacks_controller.rb"
  end

  test "Assert all controllers are properly created with scope param" do
    run_generator %w(users)
    assert_class_names 'users'

    run_generator %w(admins)
    assert_class_names 'admins'
  end

  test "Assert specified controllers with scope" do
    run_generator %w(users -c sessions)
    assert_file "app/controllers/users/sessions_controller.rb"
    assert_no_file "app/controllers/users/registrations_controller.rb"
    assert_no_file "app/controllers/users/confirmations_controller.rb"
    assert_no_file "app/controllers/users/passwords_controller.rb"
    assert_no_file "app/controllers/users/unlocks_controller.rb"
    assert_no_file "app/controllers/users/omniauth_callbacks_controller.rb"
  end

  private

    def assert_class_names(scope, options = {})
      base_dir = "app/controllers#{scope.blank? ? '' : ('/' + scope)}"
      scope_prefix = scope.blank? ? '' : (scope.camelize + '::')
      controllers = options[:controllers] ||
        %w(confirmations passwords registrations sessions unlocks omniauth_callbacks)

      controllers.each do |c|
        assert_file "#{base_dir}/#{c}_controller.rb", /#{scope_prefix + c.camelize}/
      end
    end
end