How do I test a file upload in rails? How do I test a file upload in rails? ruby-on-rails ruby-on-rails

How do I test a file upload in rails?


Searched for this question and could not find it, or its answer on Stack Overflow, but found it elsewhere, so I'm asking to make it available on SO.

The rails framework has a function fixture_file_upload (Rails 2 Rails 3, Rails 5), which will search your fixtures directory for the file specified and will make it available as a test file for the controller in functional testing. To use it:

1) Put your file to be uploaded in the test in your fixtures/files subdirectory for testing.

2) In your unit test you can get your testing file by calling fixture_file_upload('path','mime-type').

e.g.:

bulk_json = fixture_file_upload('files/bulk_bookmark.json','application/json')

3) call the post method to hit the controller action you want, passing the object returned by fixture_file_upload as the parameter for the upload.

e.g.:

post :bookmark, :bulkfile => bulk_json

Or in Rails 5: post :bookmark, params: {bulkfile: bulk_json}

This will run through the simulated post process using a Tempfile copy of the file in your fixtures directory and then return to your unit test so you can start examining the results of the post.


Mori's answer is correct, except that in Rails 3 instead of "ActionController::TestUploadedFile.new" you have to use "Rack::Test::UploadedFile.new".

The file object that is created can then be used as a parameter value in Rspec or TestUnit tests.

test "image upload" do  test_image = path-to-fixtures-image + "/Test.jpg"  file = Rack::Test::UploadedFile.new(test_image, "image/jpeg")  post "/create", :user => { :avatar => file }  # assert desired results  post "/create", :user => { :avatar => file }       assert_response 201  assert_response :successend


I think it's better to use the new ActionDispatch::Http::UploadedFile this way:

uploaded_file = ActionDispatch::Http::UploadedFile.new({    :tempfile => File.new(Rails.root.join("test/fixtures/files/test.jpg"))})assert model.valid?

This way you can use the same methods you are using in your validations (as for example tempfile).