Skip to content

Controller tests no longer accept ActionDispatch::Http::UploadedFile #26404

@kirs

Description

@kirs

In the process of upgrading to Rails 5, I've discovered a piece of code that worked fine on 4.2 and no longer works on 5-0-stable:

class PostsController < ApplicationController
  def upload
    raise "content: #{params[:file]}, class: #{params[:file].class}"
  end
end

class PostsControllerTest < ActionController::TestCase
  test "upload" do
    artwork = ActionDispatch::Http::UploadedFile.new(
      filename: 'artwork.jpeg',
      content_type: 'image/jpeg',
      tempfile: StringIO.new,
    )

    post :upload, params: { file: artwork }
  end
end
$ rails test test/controllers/posts_controller_test.rb
# Running:
Error:
PostsControllerTest#test_upload:
RuntimeError: content: #<ActionDispatch::Http::UploadedFile:0x007fb0c877acd8>, class: String
    app/controllers/posts_controller.rb:16:in `upload'
    test/controllers/posts_controller_test.rb:18:in `block in <class:PostsControllerTest>'

As you see, submitting a form with ActionDispatch::Http::UploadedFile instance gets the upload casted to String.

The reason why it happens is here: https://github.com/rails/rails/blob/5-0-stable/actionpack/lib/action_controller/test_case.rb#L137

It makes sense that Rails 5 is expecting Rack::Test::UploadedFile in test env, but I think that we shouldn't have break code that worked on 4.2. It seems like a regression to me.

cc @tenderlove since you've introduced that line.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions