rspec-rails: fixture_file_upload cannot find files in request specs

Steps to to reproduce (using rails 4.2.5.1):

rails new fixture_file_upload
cd fixture_file_upload
echo "gem 'rspec-rails', '~> 3.0'" >> Gemfile
bundle
rails generate rspec:install
mkdir -p spec/fixtures/files
touch spec/fixtures/files/test.jpg
mkdir spec/requests

Create spec/requests/test_spec.rb with the following contents:

require 'rails_helper'

describe 'test', type: :request do
  it 'tests' do
    fixture_file_upload('test.jpg')
  end
end
rake
Failures:

  1) test tests
     Failure/Error: fixture_file_upload('test.jpg')

     RuntimeError:
       test.jpg file does not exist

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 2
  • Comments: 19 (4 by maintainers)

Commits related to this issue

Most upvoted comments

The include ActionDispatch::TestProcess section works for me using Rails 5.0.0.1 and RSpec rails - 3.5.2.

Thanks!

For what it’s worth, in case this is blocking people, passing an absolute path does work:

fixture_file_upload("#{fixture_path}/files/image.jpg")

Seems like ActionDispatch::TestProcess doesn’t have access to fixture_path so it won’t be able to prepend it to the passed file path.

Also, it’s probably obvious to everybody else, but you have to explicitly put it under the params key, you can’t path it through the path (duh):


RSpec.describe 'POST /uploads' do
  let(:file) { fixture_file_upload("#{fixture_path}/files/image.jpg") }

  it 'works' do
    post uploads_path,
      params: { uploads: { file: file } }
  end

  it 'fails' do
    post uploads_path(uploads: { file: file })
  end
end

Same problem here. I found out the following while poking around:

So somehow the context inside fixture_file_upload seems to be wrong?

can be closed