How do I test a file upload in rails?

后端 未结 7 510
轻奢々
轻奢々 2020-12-07 09:29

I have a controller which is responsible for accepting JSON files and then processing the JSON files to do some user maintenance for our application. In user testing the fil

7条回答
  •  暖寄归人
    2020-12-07 10:07

    if you are getting the file in your controller with the following

    json_file = params[:json_file]
    FileUtils.mv(json_file.tempfile, File.expand_path('.')+'/tmp/newfile.json')
    

    then try the following in your specs:

    json_file = mock('JsonFile')
    json_file.should_receive(:tempfile).and_return("files/bulk_bookmark.json")
    post 'import', :json_file => json_file
    response.should be_success
    

    This will make the fake method to 'tempfile' method, which will return the path to the loaded file.

提交回复
热议问题