3.1rc8 - posting files in a test no longer works

A previously working test, that posted a file object no longer works in the latest 3.1. The test looks like this:

params[:media][:new_attachment] = File.open("#{Rails.root}/test/data/ less_than_100.png") post :create, params

In the controller, I obviously expect params[:media][:new_attachment] to be a File object - and it used to be. Now it looks like #inspect has been called on the file. Debugging out in the test:

(rdb:1) params[:media].try(:, :new_attachment) "#<File:0x007fcd0a950658>" (rdb:1) params[:media].try(:, :new_attachment).class String

Yep, what's up with that?

In the controller, I obviously expect params[:media][:new_attachment] to be a File object

Why would you expect that? Data that is posted (or sent in a get request) is a string. You aren't going to get a ruby File object in a real post request, so it makes sense to me that a test wouldn't give you a File object.

A previously working test, that posted a file object no longer works in the latest 3.1. The test looks like this:

params[:media][:new_attachment] = File.open("#{Rails.root}/test/data/ less_than_100.png") post :create, params

In the controller, I obviously expect params[:media][:new_attachment] to be a File object - and it used to be. Now it looks like #inspect has been called on the file. Debugging out in the test:

Rails 3.1 enforces that parameters should look like what you'll actually get in a test - you used to be able to stick arbitrary objects in there which of course doesn't reflect what happens in the real world. fixture_file_upload should still work

Fred

Rails 3.1 enforces that parameters should look like what you'll actually get in a test - you used to be able to stick arbitrary objects in there which of course doesn't reflect what happens in the real world. fixture_file_upload should still work

I have a similar problem. Until now I used to assign a ActionDispatch::Http::UploadedFile to my parameter, which now doesn't work anymore in rails 3.1.

Unfortunately I cannot use fixture_file_upload because it creates a Rack::Test::UploadedFile, which doesn't provide #tempfile:

params[:file].class

=> Rack::Test::UploadedFile

params[:file].tempfile

NoMethodError: undefined method `tempfile' for #<Tempfile:0xb6591b70>

There already is a bug report about this problem with fixture_file_upload: fixture_file_upload behavior · Issue #799 · rails/rails · GitHub

fixture_file_upload should still work

Worked for me. Thanks.

I have a similar problem. Until now I used to assign a ActionDispatch::Http::UploadedFile to my parameter, which now doesn't work anymore in rails 3.1.

Unfortunately I cannot use fixture_file_upload because it creates a Rack::Test::UploadedFile, which doesn't provide #tempfile:

> params[:file].class => Rack::Test::UploadedFile > params[:file].tempfile NoMethodError: undefined method `tempfile' for #<Tempfile:0xb6591b70>

It doesn't have a tempfile method but it does actually wrap a Tempfile and has a @tempfile instance variable. In the past I've reopened Rack::Test::UploadedFile to add a tempfile method

Fred