Rspec-rails: Using fixture_file_upload with rspec-rails

Created on 6 Jan 2021  ·  3Comments  ·  Source: rspec/rspec-rails

What Ruby, Rails and RSpec versions are you using?

Ruby version: 2.7.2
Rails version: 6.1.0
RSpec version: 3.10.1 (rspec-core) and 4.0.2 (rspec-rails)

Observed behaviour

Hello! I am trying to upgrade my app to Rails 6.1 from 6.0.3.4, and I'm encountering an error when using the fixture_file_upload method. When I call this method, the following exception is raised:

     NoMethodError:
       undefined method `file_fixture_path' for RSpec::Rails::FixtureFileUploadSupport::RailsFixtureFileWrapper:Class
       Did you mean?  fixture_path

It seems that ActionPack has changed the implementation of the fixture_file_upload and expects there to be a file_fixture_path method on the RailsFixtureFileWrapper class.

Expected behaviour

I expect this method to upload the file without raising any exceptions.

Can you provide an example app?

You can see my example app with a failing test here: https://github.com/egiurleo/fixture-file-upload-test/blob/main/spec/fixture_file_upload_spec.rb

For the test, I have a pdf file in the spec/fixtures folder. I am trying to call the fixture_file_upload method with the name of that file, and the test raises the exception I described earlier.

Notes

  • I may just be using this method wrong, but I can't find any documentation that explains how to set file_fixture_path in this context so any guidance would be appreciated.
  • The actual change was in ActionPack (lib/action_dispatch/testing/test_process.rb).
  • Thank you for your time!

Most helpful comment

Thanks for reporting. We're actively working on rspec-rails 4.1, you can access it by pointing to a branch:

# Gemfile
gem 'rspec-rails', github: 'rspec/rspec-rails', branch: 'rails-6-1-dev'

The fix is included there.

Sorry for the inconvenience.

Please feel free to report back if it still doesn't work for you.

All 3 comments

Thanks for reporting. We're actively working on rspec-rails 4.1, you can access it by pointing to a branch:

# Gemfile
gem 'rspec-rails', github: 'rspec/rspec-rails', branch: 'rails-6-1-dev'

The fix is included there.

Sorry for the inconvenience.

Please feel free to report back if it still doesn't work for you.

Thank you so much for letting me know!

As a temporary workaround for this, rather than fiddling with my Gemfile and making sure I had all the dependencies, I just defined my own fixture_file_upload method after looking at the source code for that method:

def fixture_file_upload(path, mime_type)
  Rack::Test::UploadedFile.new(Pathname.new(file_fixture_path).join(path), mime_type, false)
end
Was this page helpful?
0 / 5 - 0 ratings