test a file upload using rspec – rails

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

Leave a Comment