Rails 5集成测试将FIXTURE_FILE_UPLOAD转换为字符串



env
铁轨:5.1.3
Ruby:2.3.1p112

上下文
我的基本集成测试如下:

class MerchandisesControllerTest < ActionDispatch::IntegrationTest
  def test_successful_create
    post org_merchandises_path(org, merchandise: merchandise_params)
    ...
  end
  def merchandise_params
    {
      description: 'Get swole fast.',
      name: 'Foo Powder',
      price: 50,
      images_attributes: {
        '0' => {
          image:   fixture_file_upload('files/image.png', 'image/png'),
          primary: true,
        }
      }
    }
  end
end

当我运行测试时,图像参数将转换为 String

# Inside MerchandisesController#create
params.dig(:merchandise, :images_attributes, '0', :image)
=> "#<Rack::Test::UploadedFile:0x007fbdc9520b00>"
# This is a String, but should instance of ActionDispatch::Http::UploadedFile

可能的相关问题
https://github.com/rails/rails/issues/28129

出于某种原因,以以下方式使用邮政方法无法正确序列化/估算图像param:

# Fails to properly serialize file param
post org_merchandises_path(org, merchandise: merchandise_params)

您必须这样做:

# Properly serializes the file param
post org_merchandises_path(org), params: { merchandise: merchandise_params }

我不知道为什么。似乎应该两种工作,但事实并非如此。

最新更新