【问题标题】:How do I test a file upload in rails for integration testing?如何在 Rails 中测试文件上传以进行集成测试?
【发布时间】:2012-06-11 10:15:34
【问题描述】:

我做了一些这样的集成测试:

 def user.excel_import
          fixture_excel = fixture_file_upload('goodsins.xls', 'text/xls')
          post excel_import_goods_ins_goods_ins_path, :dump=> {:excel_file=>fixture_excel}, :html => { :multipart => "true" }
          assert_response :redirect
          assert_redirected_to goods_ins_path
       end

但是当我运行测试时说:goodins.xls 文件不存在。 仅供参考:我将文件放在名为 fixtures 的文件夹中。

有什么想法吗?非常感谢你

【问题讨论】:

  • 这种测试传统上称为控制器测试。

标签: ruby-on-rails integration-testing fixtures


【解决方案1】:

此处的注释:http://apidock.com/rails/ActionController/TestProcess/fixture_file_upload 表示您需要在路径或文件名前包含一个斜杠。

试试fixture_file_upload('/goodsins.xls', 'text/xls') 看看是否有帮助。

fixture_file_upload 来源:

# File actionpack/lib/action_controller/test_process.rb, line 523
def fixture_file_upload(path, mime_type = nil, binary = false)
  if ActionController::TestCase.respond_to?(:fixture_path)
    fixture_path = ActionController::TestCase.send(:fixture_path)
  end

  ActionController::TestUploadedFile.new("#{fixture_path}#{path}",
    mime_type, binary)
end

来自问题所有者的更新:

解决方案:

include ActionDispatch::TestProcess 添加到test_helper.rb

【讨论】:

  • 是的,我已经在路径或文件名之前使用了斜杠,但它仍然是文件不存在..在功能测试中它运行良好,但在集成测试中它不起作用.. .
  • 但是你的链接对我来说非常有用......感谢我......解决方案只是将这个 include ActionDispatch::TestProcess 添加到以下 test_helper.rb
猜你喜欢
  • 2015-01-24
  • 2010-11-13
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-30
相关资源
最近更新 更多