如何在 Rails Rspec 请求规范中上传文件 [英] How to upload a file in a Rails Rspec request spec
本文介绍了如何在 Rails Rspec 请求规范中上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 Rails Rspec 请求测试中测试上传文件.我正在使用 Paperclip 进行资产存储.
I want to test uploading a file in a Rails Rspec request test. I'm using Paperclip for asset storage.
我试过了:
path = 'path/to/fixture_file'
params = { file: Rack::Test::UploadedFile.new(path, 'application/pdf', true) }
post v1_product_documents_path, params: params
在控制器中,我得到一个字符串
In the controller, I get a string
"#Rack::Test::UploadedFile:0x0055b544479128>"
"#Rack::Test::UploadedFile:0x0055b544479128>"
而不是实际文件.
相同的代码适用于控制器测试
The same code works in controller tests
推荐答案
尝试使用fixture_file_upload:fixture_file_upload
或如果你想在工厂中使用它
or if you wanted to use this in a factory
Rack::Test::UploadedFile.new(File.open(File.join(Rails.root, '/spec/fixtures/images/bob-weir.jpg')))
这篇关于如何在 Rails Rspec 请求规范中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文