ActionDispatch::Http::UploadedFile 在集成测试期间作为字符串传递

ActionDispatch::Http::UploadedFile being passed as a string during integration test

我有一个相册模型和一个子图片模型,文件上传由载波处理。

应用程序按预期运行,但是当我尝试为上传编写集成测试时失败。

从开发日志来看,其工作参数如下:

Parameters: ..., "images"=>[#<ActionDispatch::Http::UploadedFile:0x007f8a42be2c78>...

集成测试时,我得到的参数如下:

Parameters: ..., "images"=>["#<ActionDispatch::Http::UploadedFile:0x00000004706fd0>"...

如您所见,在测试场景中,#<ActionDispatch::Http::UploadedFile 作为字符串传递(通过 byebug 确认),这导致测试失败。

如何让测试通过 this 作为对象?

综合测试

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

来自 test_helper.rb

# 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

型号

class Album < 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 < 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

控制器

class AlbumsController < 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

已解决:

看来 uploaded_file_object 方法在集成测试级别不起作用。

我重新使用 fixture_file_upload 并且一切正常。