r/rails•
exige1981

[Help] ActionDispatch::Http::UploadedFile is being passed as a string during integration test.

Help

When running the working app, the params from my development log are along the lines of Parameters: ..., "images"=>[#<ActionDispatch::Http::UploadedFile:0x007f8a42be2c78>

In my integration test, im getting something like Parameters: ..., "images"=>["#<ActionDispatch::Http::UploadedFile:0x00000004706fd0>" So instead of the UploadedFile object, i'm getting a string of the name of the uploaded file object instead.

my integration test is as follows:

require 'test_helper'
class AlbumsCreationTest <     ActionDispatch::IntegrationTest
  
  def setup
    @user  = users(:archer)
    @file ||= File.open(File.expand_path(Rails.root + 'test/fixtures/cat1.jpg', __FILE__))
    @testfile  ||= uploaded_file_object(PicturesUploader, :image, @file)
  end
  
  test "should create new album with valid info" do
    log_in_as(@user)
    assert_difference 'Album.count', 1 do  #This assertion fails
      post user_albums_path(@user), 
          album: { title:           'test',
                        description: 'test',
                      }, images: [@testfile]
    end
    assert_redirected_to @user
  end
end

the uploaded_file_object method comes from http://nicholshayes.co.uk/blog/?p=405 and is stored in my test_helper.rb

Whats causing the test to pass this object as a string?


EDIT: [SOLVED]

it seems that the uploaded_file_object method described here doesnt cut it at the integration test level, so i've switched to fixture_file_upload and everything works as intended (I had tried this previously without success, must have been some other error). Note that when using fixture_file_upload, adding multipart: true is not required.

Thanks all for helping this noob out.

Posted May 29, 2016, 8:16 PM UTC · edited

68
Loading comments…