Back to Repositories

Testing CarrierWave File Upload Integration in Rails Admin

This test suite validates CarrierWave file upload functionality within the Rails Admin interface. It focuses on testing file upload caching behavior and form submission validation, ensuring proper integration between CarrierWave and Rails Admin forms.

Test Coverage Overview

The test coverage focuses on the CarrierWave file upload field integration in Rails Admin forms.

Key areas tested include:
  • File upload functionality with caching
  • Form validation behavior
  • Persistence of uploaded files
  • Error handling during invalid form submissions

Implementation Analysis

The testing approach utilizes RSpec’s request specs to simulate real user interactions with the file upload interface. The implementation leverages RSpec’s page object pattern and form interaction helpers to validate the CarrierWave integration, specifically testing the caching mechanism that preserves uploaded files across form submissions.

Technical patterns include:
  • Request-type integration testing
  • File attachment simulation
  • Form submission validation
  • File system verification

Technical Details

Testing tools and configuration:
  • RSpec for test framework
  • Capybara for page interaction
  • CarrierWave for file upload handling
  • Rails Admin configuration blocks
  • Active Record integration
  • File system verification helpers

Best Practices Demonstrated

The test suite exemplifies several testing best practices for file upload functionality.

Notable practices include:
  • Isolation of file upload behavior
  • Proper setup and teardown of test environment
  • Validation of both happy path and error scenarios
  • Clear test organization and descriptive contexts
  • Effective use of RSpec’s expectation syntax

railsadminteam/rails_admin

spec/integration/fields/carrierwave_spec.rb

            
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Carrierwave field', type: :request, active_record: true do
  subject { page }
  before do
    RailsAdmin.config FieldTest do
      edit do
        field :string_field
        field :carrierwave_asset
      end
    end
  end

  it 'supports caching an uploaded file' do
    visit new_path(model_name: 'field_test')
    attach_file 'Carrierwave asset', file_path('test.jpg')
    fill_in 'field_test[string_field]', with: 'Invalid'
    click_button 'Save'
    expect(page).to have_content 'Field test failed to be created'
    fill_in 'field_test[string_field]', with: ''
    click_button 'Save'
    expect(FieldTest.first.carrierwave_asset.file).to exist
  end
end