Back to Repositories

Testing Shrine File Upload Integration in rails_admin

This test suite validates Shrine file upload integration within Rails Admin, focusing on file caching behavior and form submission scenarios. The tests ensure proper handling of file attachments during form validation and persistence.

Test Coverage Overview

The test suite covers essential file upload functionality using Shrine in Rails Admin:

  • File attachment validation and caching
  • Form submission with invalid data handling
  • Persistence of uploaded files after validation errors
  • Integration between Shrine and ActiveRecord models

Implementation Analysis

The testing approach utilizes RSpec request specs with JavaScript support to simulate real user interactions. The implementation focuses on the field configuration within Rails Admin and validates the complete file upload workflow, including error scenarios and successful submissions.

Technical Details

  • RSpec for test framework
  • Capybara for browser simulation
  • JavaScript-enabled testing
  • Shrine for file upload handling
  • Rails Admin configuration specs
  • ActiveRecord integration testing

Best Practices Demonstrated

The test exhibits several testing best practices including isolation of concerns, proper setup of test environment, and comprehensive validation scenarios. It demonstrates effective use of RSpec’s request specs, proper configuration of test dependencies, and validation of both positive and negative test cases.

railsadminteam/rails_admin

spec/integration/fields/shrine_spec.rb

            
# frozen_string_literal: true

require 'spec_helper'

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

  it 'supports caching an uploaded file', js: true do
    visit new_path(model_name: 'field_test')
    attach_file 'Shrine 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.shrine_asset).to exist
  end
end