ActionDispatch::Http::UploadedFile being passed as a string during integration test
<p>I have an Album model and a child Picture model, whereby a file upload is handled by carrierwave. </p>

<p>The app runs as intended, however fails when I try to write an integration test for the upload.  </p>

<p>From the development log, working parameters for this appear as follows:</p>

<p><code>Parameters: ..., "images"=&gt;[#&lt;ActionDispatch::Http::UploadedFile:0x007f8a42be2c78&gt;...</code></p>

<p>During the integration test, im getting parameters as follows:</p>

<p><code>Parameters: ..., "images"=&gt;["#&lt;ActionDispatch::Http::UploadedFile:0x00000004706fd0&gt;"...</code></p>

<p>As you can see, in the test scenario, <code>#&lt;ActionDispatch::Http::UploadedFile</code> is being passed through as a string (confirmed with byebug), which is causing the test to fail.  </p>

<p>How do I get the test to pass this as the object?</p>

<p><strong>Integration Test</strong></p>

<pre><code>require 'test_helper'
class AlbumsCreationTest &lt; 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
</code></pre>

<p>from test_helper.rb </p>

<pre><code># Upload File (Carrierwave) Ref: http://nicholshayes.co.uk/blog/?p=405
def uploaded_file_object(klass, attribute, file, content_type = 'image/jpeg')

filename = File.basename(file.path)
klass_label = klass.to_s.underscore

ActionDispatch::Http::UploadedFile.new(
  tempfile: file,
  filename: filename,
  head: %Q{Content-Disposition: form-data; name="#{klass_label}[#{attribute}]"; filename="#{filename}"},
  type: content_type
)
end
</code></pre>

<p><strong>Models</strong></p>

<pre><code>class Album &lt; ActiveRecord::Base
  belongs_to :user
  has_many :pictures, dependent: :destroy
  accepts_nested_attributes_for :pictures, allow_destroy: true

  validates_presence_of :pictures
end

class Picture &lt; ActiveRecord::Base
  belongs_to :album
  mount_uploader :image, PicturesUploader

  validates_integrity_of  :image
  validates_processing_of :image
  validates :image, presence: true,
                    file_size: { less_than: 10.megabytes }
end
</code></pre>

<p><strong>Controller</strong></p>

<pre><code>class AlbumsController &lt; ApplicationController
  before_action :valid_user, only: [:new, :create, :edit, :update, :destroy]

  def create
    @album = current_user.albums.build(album_params)
    if params[:images]
        params[:images].each { |file|
          debugger #file.class is String in integration test
          @album.pictures.build(image: file)
        }
    end
    if @album.save
      # end
      flash[:success] = "Album Created!"
      redirect_to current_user
    else 
      flash[:alert] = "Something went wrong."
      render :new
    end
  end

  private

    def album_params
      params.require(:album).permit(:user_id, :title, :description, :price, 
        pictures_attributes: [:id, :image, :image_cache, :_destroy])   
    end

    def valid_user
      @user = User.friendly.find(params[:user_id])
      redirect_to(root_url) unless @user == current_user
    end
end
</code></pre>