test a file upload using rspec - rails

后端 未结 6 2064
梦毁少年i
梦毁少年i 2020-11-27 09:52

I want to test a file upload in rails, but am not sure how to do this.

Here is the controller code:

def uploadLicense
    #Create the license object
         


        
6条回答
  •  失恋的感觉
    2020-11-27 10:26

    I am not sure if you can test file uploads using RSpec alone. Have you tried Capybara?

    It's easy to test file uploads using capybara's attach_file method from a request spec.

    For example (this code is a demo only):

    it "can upload a license" do
      visit upload_license_path
      attach_file "uploadLicense", /path/to/file/to/upload
      click_button "Upload License"
    end
    
    it "can download an uploaded license" do
      visit license_path
      click_link "Download Uploaded License"
      page.should have_content("Uploaded License")
    end
    

提交回复
热议问题