我使用這個代碼塊來模仿文件上傳方式:RSpec將POST參數轉換爲字符串? (測試文件上傳)
def mock_file
file = File.new((Rails.root + "public/checklist_items_template.csv"),"r")
image = ActionDispatch::Http::UploadedFile.new(
:filename => "checklist_items_template.csv",
:type => "text/csv",
:head => "Content-Disposition: form-data;
name=\"checklist_items_template.csv\";
filename=\"checklist_items_template.csv\"
Content-Type: text/csv\r\n",
:tempfile => file)
return image
end
在RSpec的測試是POST'd控制器:
post :create, :legal_register_id => "1", :register => {"file" => mock_file}
但它打破這條線在實際控制:
CSV.parse(params[:register][:file].read.force_encoding('UTF-8'))
由於PARAMS [:寄存器] [:文件]被interpretted作爲字符串而不是actiondispatch對象:
undefined method `read' for "#<ActionDispatch::Http::UploadedFile:0x00000108de3da8>":String
這是rspec的標準行爲嗎?有沒有辦法通過params傳遞對象?
所以...你有沒有得到它的工作?我的回答有幫助嗎? –