Back to Repositories

Testing Mongoid Generator Integration in Devise Framework

This test suite validates the Mongoid generator functionality for Devise authentication in Rails applications. It ensures proper model generation and cleanup when using Devise with Mongoid ORM, focusing on both creation and deletion of authentication-related files.

Test Coverage Overview

The test suite provides essential coverage for the Mongoid::Generators::DeviseGenerator functionality.

Key areas tested include:
  • Model file generation with proper Devise integration
  • Proper cleanup and file removal during generator revocation
  • Verification of generator behavior with the ‘monster’ test model

Implementation Analysis

The testing approach utilizes Rails::Generators::TestCase as the foundation for generator testing. It implements a straightforward pattern of generation and revocation verification, leveraging Minitest’s assertion methods to confirm file presence and content.

Technical implementation details:
  • Conditional testing based on DEVISE_ORM configuration
  • File path expansion for temporary test directories
  • Generator behavior testing for both creation and deletion scenarios

Technical Details

Testing tools and configuration:
  • Minitest framework integration
  • Rails::Generators::TestCase for generator testing
  • Temporary directory setup for test isolation
  • Mongoid ORM integration checking
  • File system manipulation for model creation and deletion

Best Practices Demonstrated

The test suite exemplifies several testing best practices for generator validation.

Notable practices include:
  • Isolation of generator tests using temporary directories
  • Proper setup and teardown management
  • Clear test naming conventions
  • Explicit assertion checking for file existence and content
  • Comprehensive testing of both positive and negative scenarios

heartcombo/devise

test/generators/mongoid_generator_test.rb

            
# frozen_string_literal: true

require "test_helper"

if DEVISE_ORM == :mongoid
  require "generators/mongoid/devise_generator"

  class MongoidGeneratorTest < Rails::Generators::TestCase
    tests Mongoid::Generators::DeviseGenerator
    destination File.expand_path("../../tmp", __FILE__)
    setup :prepare_destination

    test "all files are properly created" do
      run_generator %w(monster)
      assert_file "app/models/monster.rb", /devise/
    end

    test "all files are properly deleted" do
      run_generator %w(monster)
      run_generator %w(monster), behavior: :revoke
      assert_no_file "app/models/monster.rb"
    end
  end
end