Back to Repositories

Testing Dragonfly Field Type Integration in Rails Admin

This test suite validates the Dragonfly field type implementation in Rails Admin, focusing on image handling and model integration. The tests ensure proper functionality of Dragonfly assets within the Rails Admin interface while maintaining compatibility with both image and non-image file types.

Test Coverage Overview

The test suite provides comprehensive coverage of the Dragonfly field type functionality in Rails Admin.

Key areas tested include:
  • Generic field type behavior verification
  • Image type detection and validation
  • Non-image file handling
  • Model compatibility with non-Dragonfly extensions

Implementation Analysis

The testing approach utilizes RSpec’s shared examples pattern for consistent field type validation. The implementation focuses on isolated unit tests that verify both the core functionality and edge cases of Dragonfly integration.

Key patterns include:
  • Shared behavior testing using it_behaves_like
  • Context-specific test scenarios
  • Factory-based test data generation
  • File-based fixture handling

Technical Details

Testing tools and configuration:
  • RSpec as the primary testing framework
  • FactoryBot for test data generation
  • File fixtures for asset testing
  • Tableless model support for isolation testing
  • Custom field configuration using RailsAdmin.config

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Ruby and Rails development.

Notable practices include:
  • Isolation of test scenarios using appropriate contexts
  • Proper setup and teardown of test data
  • Clear separation of concerns in test organization
  • Effective use of shared examples for consistent behavior verification
  • Comprehensive edge case coverage

railsadminteam/rails_admin

spec/rails_admin/config/fields/types/drangonfly_spec.rb

            
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe RailsAdmin::Config::Fields::Types::Dragonfly do
  it_behaves_like 'a generic field type', :string_field, :dragonfly

  let(:field) do
    RailsAdmin.config('FieldTest').fields.detect do |f|
      f.name == :dragonfly_asset
    end.with(object: record)
  end

  describe '#image?' do
    let(:file) { File.open(file_path('test.jpg')) }
    let(:record) { FactoryBot.create :field_test, dragonfly_asset: file }

    it 'returns true' do
      expect(field.image?).to be true
    end

    context 'with non-image' do
      let(:file) { File.open(file_path('test.txt')) }

      it 'returns false' do
        expect(field.image?).to be false
      end
    end
  end

  describe 'with a model which does not extend Dragonfly::Model' do
    before do
      class NonDragonflyTest < Tableless
        column :asset_uid, :varchar
      end
    end

    it 'does not break' do
      expect { RailsAdmin.config(NonDragonflyTest).fields }.not_to raise_error
    end
  end
end