rspec/rspec-rails

View on GitHub
lib/rspec/rails/fixture_file_upload_support.rb

Summary

Maintainability
A
25 mins
Test Coverage
module RSpec
  module Rails
    # @private
    module FixtureFileUploadSupport
      delegate :fixture_file_upload, to: :rails_fixture_file_wrapper

    private

      def rails_fixture_file_wrapper
        RailsFixtureFileWrapper.fixture_path = nil
        resolved_fixture_path =
          if respond_to?(:fixture_path) && !fixture_path.nil?
            fixture_path.to_s
          else
            (RSpec.configuration.fixture_path || '').to_s
          end
        RailsFixtureFileWrapper.fixture_path = File.join(resolved_fixture_path, '') unless resolved_fixture_path.strip.empty?
        RailsFixtureFileWrapper.instance
      end

      class RailsFixtureFileWrapper
        include ActionDispatch::TestProcess if defined?(ActionDispatch::TestProcess)

        class << self
          attr_reader :fixture_path

          # Get instance of wrapper
          def instance
            @instance ||= new
          end

          # Override fixture_path set
          # to support Rails 3.0->3.1 using ActionController::TestCase class to resolve fixture_path
          # see https://apidock.com/rails/v3.0.0/ActionDispatch/TestProcess/fixture_file_upload
          def fixture_path=(value)
            if ActionController::TestCase.respond_to?(:fixture_path)
              ActionController::TestCase.fixture_path = value
            end
            @fixture_path = value
          end
        end
      end
    end
  end
end