test a file upload using rspec - rails

后端 未结 6 2060
梦毁少年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:25

    You can use fixture_file_upload method to test file uploading: Put your test file in "{Rails.root}/spec/fixtures/files" directory

    before :each do
      @file = fixture_file_upload('files/test_lic.xml', 'text/xml')
    end
    
    it "can upload a license" do
      post :uploadLicense, :upload => @file
      response.should be_success
    end
    

    In case you were expecting the file in the form of params['upload']['datafile']

    it "can upload a license" do
      file = Hash.new
      file['datafile'] = @file
      post :uploadLicense, :upload => file
      response.should be_success
    end
    

提交回复
热议问题