6

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

 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 的文件夹中。

任何的想法?非常感谢你

4

1 回答 1

9

这里的注释: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::TestProcesstest_helper.rb

于 2012-06-11T13:06:54.650 回答