Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fixture_file_upload has {file} does not exist error

Below is my testing code for uploading file.

describe "file process" do
 before(:each) do
   # debugger
   @file = fixture_file_upload('test.csv', 'text/csv')
 end

 it "should be able to upload file" do
  post :upload_csv, :upload => @file
  response.should be_success
 end
end

However, when I run rspec spec, it produced me the error below

Failure/Error: @file = fixture_file_upload('test.csv', 'text/csv')
 RuntimeError:
   test.csv file does not exist
 # ./spec/controllers/quotation_controller_spec.rb:29:in `block (3 levels) in <top (required)>'

I have googled alot of places but I still couldn't find out what's the reason behind it. Any idea?

like image 984
hanchee Avatar asked Jan 25 '12 22:01

hanchee


2 Answers

Fixture_file_upload basically still works. You just need to make sure the fixtures path in your spec_helper.rb file is uncommented & correctly set to the spec/fixtures path & to include ActionDispath::TestProcess:

RSpec.configure do |config|
  config.include ActionDispatch::TestProcess

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  ...

And where you are specifying the file in your tests, be sure to precede your file name with '/' like the following example:

describe "POST /subscriber_imports" do
  let(:file) { { :file => fixture_file_upload('/files/data.csv', 'text/csv') } }
  subject { post :create, :subscriber_import => file }
  ...
end

The absolute path to the file is the base path specified in config.fixture_path plus the relative path specified in fixture_file_upload function call. So, in this example, file.csv must be placed in #{::Rails.root}/spec/fixtures/files/data.csv

like image 66
Adrian Teh Avatar answered Oct 23 '22 00:10

Adrian Teh


This is how I did it with Rails 6, RSpec and Rack::Test::UploadedFile

describe 'POST /create' do
  it 'responds with success' do
    post :create, params: {
      company: {
        logo: Rack::Test::UploadedFile.new("#{Rails.root}/spec/fixtures/test-pic.png"),
        name: 'test'
      }
    }

    expect(response).to be_successful
  end
end

DO NOT include ActionDispatch::TestProcess or any other code unless you're sure about what you're including.

like image 28
thisismydesign Avatar answered Oct 23 '22 01:10

thisismydesign