Ruby on rails 如何在rails中测试文件上载以进行集成测试?
我做了一些集成测试,如下所示:Ruby on rails 如何在rails中测试文件上载以进行集成测试?,ruby-on-rails,integration-testing,fixtures,Ruby On Rails,Integration Testing,Fixtures,我做了一些集成测试,如下所示: 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" }
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
但是当我运行测试时,它说:goodsins.xls文件不存在。
仅供参考:我将文件放在名为fixtures的文件夹中
有什么想法吗?非常感谢此处的注释:表示需要在路径或文件名之前包含斜杠
尝试fixture\u file\u上传('/goodsins.xls',text/xls')
看看这是否有帮助
夹具文件上传源:
# 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\u helper.rb
是的,我已经在路径或文件名之前使用了斜杠,但它仍然不存在。。。。在功能测试上它工作得很好,但是在集成测试上它不工作…但是你的链接对我来说非常有用。。。给我的thx。。。解决方案就是将这个include ActionDispatch::TestProcess添加到下面的test\u helper.rb中。这种测试传统上称为控制器测试。