I am using this block of code to mimic the way files are uploaded:
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
In the rspec test it is POST'd to the controller:
post :create, :legal_register_id => "1", :register => {"file" => mock_file}
But it breaks this line in the actual controller:
CSV.parse(params[:register][:file].read.force_encoding('UTF-8'))
Because params[:register][:file] is being interpretted as a string instead of an actiondispatch object:
undefined method `read' for "#<ActionDispatch::Http::UploadedFile:0x00000108de3da8>":String
Is this standard behavior for rspec? Is there a way to pass objects via params?