Back to Repositories

Testing Devise Generator Route Configuration in heartcombo/devise

This test suite validates the Devise generator functionality in Rails applications, focusing on route generation for different model configurations and namespace scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of Devise generator’s route generation capabilities.

  • Tests route generation for simple model names
  • Validates namespaced model routing configuration
  • Verifies skip-routes functionality
  • Covers edge cases in model naming and namespace handling

Implementation Analysis

The implementation follows Rails generator testing patterns using Rails::Generators::TestCase as the base class. The tests utilize a structured approach with setup methods for destination preparation and route copying.

  • Uses Rails generator testing framework
  • Implements isolated test environment setup
  • Employs file assertion methods for route verification

Technical Details

  • Testing Framework: Minitest
  • Test Environment: Rails generator test framework
  • File Management: FileUtils for test file setup
  • Assertion Methods: File content verification
  • Configuration: Temporary destination setup for isolated testing

Best Practices Demonstrated

The test suite exemplifies strong testing practices through isolated test environments and comprehensive setup procedures.

  • Clean test environment setup and teardown
  • Explicit test case naming
  • Focused test scenarios
  • Proper file path handling
  • Isolated test file management

heartcombo/devise

test/generators/devise_generator_test.rb

            
# frozen_string_literal: true

require 'test_helper'

require "generators/devise/devise_generator"

class DeviseGeneratorTest < Rails::Generators::TestCase
  tests Devise::Generators::DeviseGenerator
  destination File.expand_path("../../tmp", __FILE__)

  setup do
    prepare_destination
    copy_routes
  end

  test "route generation for simple model names" do
    run_generator %w(monster name:string)
    assert_file "config/routes.rb", /devise_for :monsters/
  end

  test "route generation for namespaced model names" do
    run_generator %w(monster/goblin name:string)
    match = /devise_for :goblins, class_name: "Monster::Goblin"/
    assert_file "config/routes.rb", match
  end

  test "route generation with skip routes" do
    run_generator %w(monster name:string --skip-routes)
    match = /devise_for :monsters, skip: :all/
    assert_file "config/routes.rb", match
  end

  def copy_routes
    routes = File.expand_path("../../rails_app/config/routes.rb", __FILE__)
    destination = File.join(destination_root, "config")

    FileUtils.mkdir_p(destination)
    FileUtils.cp routes, destination
  end

end