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?
It's not RSpec that's converting your param objects to strings, it's Rails 3.1.
You can get around it by using fixture_file_upload as described in this answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With