Back to Repositories

Testing Devise Installation Generator Workflow in heartcombo/devise

This test suite validates the Devise installation generator functionality, ensuring proper configuration file creation and ORM dependency handling. It verifies both successful installation scenarios and error handling for missing requirements.

Test Coverage Overview

The test suite provides comprehensive coverage of the Devise installation generator’s core functionality.

  • Validates successful creation of configuration files with specified ORM
  • Tests error handling for missing ORM specification
  • Verifies proper file paths and content generation
  • Ensures localization file creation

Implementation Analysis

The testing approach utilizes Rails::Generators::TestCase as the base framework, implementing systematic validation of generator outputs.

Key patterns include:
  • Generator execution with and without parameters
  • File existence verification
  • Content pattern matching
  • Error message capture and validation

Technical Details

Testing infrastructure includes:
  • Rails Generator testing framework
  • File system manipulation utilities
  • Temporary directory setup
  • Standard assertion methods
  • Error stream capture functionality

Best Practices Demonstrated

The test suite exemplifies robust testing practices for Rails generators.

  • Proper test isolation through temporary directory setup
  • Comprehensive positive and negative test cases
  • Clear test naming conventions
  • Efficient use of Rails generator testing utilities
  • Proper error handling validation

heartcombo/devise

test/generators/install_generator_test.rb

            
# frozen_string_literal: true

require "test_helper"

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

  test "assert all files are properly created" do
    run_generator(["--orm=active_record"])
    assert_file "config/initializers/devise.rb", /devise\/orm\/active_record/
    assert_file "config/locales/devise.en.yml"
  end

  test "fails if no ORM is specified" do
    stderr = capture(:stderr) do
      run_generator
    end

    assert_match %r{An ORM must be set to install Devise}, stderr

    assert_no_file "config/initializers/devise.rb"
    assert_no_file "config/locales/devise.en.yml"
  end
end