Back to Repositories

Testing RailsAdmin Engine Configuration Reloading in rails_admin

This test suite validates the RailsAdmin Engine’s configuration reloading functionality in development mode. It specifically focuses on how the admin interface handles class unloading and configuration updates during Rails’ code reloading process.

Test Coverage Overview

The test coverage focuses on the dynamic reconfiguration of RailsAdmin fields during development.

  • Verifies field configuration persistence across reloads
  • Tests class unloading behavior
  • Ensures proper config reinitialization
  • Validates development mode specific features

Implementation Analysis

The testing approach simulates Rails’ code reloading mechanism in a controlled environment. It utilizes RSpec’s context and let blocks to maintain test isolation, while leveraging Rails’ executor and ActiveSupport::Reloader to replicate real-world development scenarios.

  • Uses before/after hooks for environment setup
  • Implements dynamic configuration changes
  • Simulates Rails application reloading

Technical Details

  • RSpec testing framework
  • Rails application executor
  • ActiveSupport::Reloader
  • RailsAdmin configuration DSL
  • Development mode cache settings

Best Practices Demonstrated

The test suite exemplifies proper testing practices for Rails engine development.

  • Proper test environment isolation
  • Clean setup and teardown
  • Explicit state management
  • Clear expectations and assertions
  • Realistic scenario simulation

railsadminteam/rails_admin

spec/rails_admin/engine_spec.rb

            
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe RailsAdmin::Engine do
  context 'on class unload' do
    let(:fields) { RailsAdmin.config(Player).edit.fields }
    before do
      Rails.application.config.cache_classes = false
      RailsAdmin.config(Player) do
        field :name
        field :number
      end
    end
    after { Rails.application.config.cache_classes = true }

    it 'triggers RailsAdmin config to be reloaded' do
      # this simulates rails code reloading
      RailsAdmin::Engine.initializers.find do |i|
        i.name == 'RailsAdmin reload config in development'
      end.block.call(Rails.application)
      Rails.application.executor.wrap do
        ActiveSupport::Reloader.new.tap(&:class_unload!).complete!
      end

      RailsAdmin.config(Player) do
        field :number
      end
      expect(fields.map(&:name)).to match_array %i[number]
    end
  end
end