Rspec-rails: file_fixture_path fails with Rails 6.1 RC1

Created on 14 Nov 2020  ·  11Comments  ·  Source: rspec/rspec-rails

What Ruby, Rails and RSpec versions are you using?

Ruby version: ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]
Rails version: Rails 6.1.0.rc1
RSpec version:

RSpec 3.10
  - rspec-core 3.10.0
  - rspec-expectations 3.10.0
  - rspec-mocks 3.10.0
  - rspec-rails 4.0.1
  - rspec-support 3.10.0

Observed behaviour

Use of fixture_file_upload results in the following error:

undefined method `file_fixture_path' for RSpec::Rails::FixtureFileUploadSupport::RailsFixtureFileWrapper:Class

Expected behaviour

When rspec-rails is used with previous Rails version, fixture_file_upload behaves as documented

Can you provide an example app?

Create a new Rails app for Rails 6.1.0.rc1 with RSpec:

gem install rails --pre
rails new rspecdemo
echo 'gem "rspec-rails", group: [:development, :test]' >> Gemfile
bundle install
rails generate rspec:install

Add the following test:

commit 65d60a8139f9856759818d95a32dcb9332589a11 (HEAD -> master)
Author: Hayden Ball <[email protected]>
Date:   Sat Nov 14 20:11:39 2020 +0000

    Add failing spec

diff --git a/spec/test_spec.rb b/spec/test_spec.rb
new file mode 100644
index 0000000..f922374
--- /dev/null
+++ b/spec/test_spec.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+require "rails_helper"
+
+RSpec.describe "test" do
+  it "fails" do
+    fixture_file_upload("test_file")
+  end
+end

Observe failure:

$ bundle exec rspec spec/test_spec.rb
F

Failures:

  1) test fails
     Failure/Error: fixture_file_upload("test_file")

     NoMethodError:
       undefined method `file_fixture_path' for RSpec::Rails::FixtureFileUploadSupport::RailsFixtureFileWrapper:Class
       Did you mean?  fixture_path
     # ./spec/test_spec.rb:7:in `block (2 levels) in <top (required)>'

Finished in 0.02347 seconds (files took 1.31 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/test_spec.rb:6 # test fails

Most helpful comment

Sorry I'd forgotten we kept this in a specific branch, my apologies, the branch for rspec-rails you want is rails-6-1-dev, you shouldn't need Github versions of the other gems to make that work.

All 11 comments

Have you tried main there are specific unreleased improvements for the unreleased Rails 6.1 version

I had not, but I now have with the following Gemfile:

gem "rspec-core", group: [:development, :test], github: "rspec/rspec-core", branch: "main"
gem "rspec-expectations", group: [:development, :test], github: "rspec/rspec-expectations", branch: "main"
gem "rspec-mocks", group: [:development, :test], github: "rspec/rspec-mocks", branch: "main"
gem "rspec-rails", group: [:development, :test], github: "rspec/rspec-rails", branch: "main"
gem "rspec-support", group: [:development, :test], github: "rspec/rspec-support", branch: "main"

(resulting in the following Gemfile.lock)

Unfortunately I'm still seeing the same failure.

Sorry I'd forgotten we kept this in a specific branch, my apologies, the branch for rspec-rails you want is rails-6-1-dev, you shouldn't need Github versions of the other gems to make that work.

That's sorted it. Thanks @JonRowe

I did find I did need GitHub versions and to cherry-pick ed754855e75fd3afd4fb7528fc9643aa2a24c516 to make Bundler happy though (https://github.com/rspec/rspec-rails/pull/2403, if you want it).

  gem "rspec", group: [:development, :test], github: "rspec/rspec", branch: "main"
  gem "rspec-core", group: [:development, :test], github: "rspec/rspec-core", branch: "main"
  gem "rspec-expectations", group: [:development, :test], github: "rspec/rspec-expectations", branch: "main"
  gem "rspec-mocks", group: [:development, :test], github: "rspec/rspec-mocks", branch: "main"
  gem "rspec-rails", github: "PlayerData/rspec-rails", branch: "rails-6-1-dev"
  gem "rspec-support", group: [:development, :test], github: "rspec/rspec-support", branch: "main"

Thanks

I'm using the latest main branches for all the rspec gems, plus rails-6-1-dev for rspec-rails and getting failures with this fixture issue still (on Rails 6.2.0.rc2):

  4) Foo bar test name
     Failure/Error: picture { fixture_file_upload('files/picture.jpg') }

     NoMethodError:
       undefined method `file_fixture_path' for #<Class:0x00007f8cb5a97128>
       Did you mean?  fixture_file_upload
     # ./spec/factories/picture_factory.rb:15:in `block (3 levels) in <top (required)>'

I'm not sure if rc2 broke it again in a different way? I've tried messing with the config.fixture_path value in a bunch of ways but nothing seems to get this working.

The error itself goes away for me with

-  gem "rspec"
-  gem "rspec-rails"
+  gem "rspec", github: "rspec/rspec", branch: "main"
+  gem "rspec-core", github: "rspec/rspec-core", branch: "main"
+  gem "rspec-mocks", github: "rspec/rspec-mocks", branch: "main"
+  gem "rspec-expectations", github: "rspec/rspec-expectations", branch: "main"
+  gem "rspec-support", github: "rspec/rspec-support", branch: "main"
+  gem "rspec-rails", github: "rspec/rspec-rails", branch: "rails-6-1-dev"

but the new version doesn't seem to support custom fixture_path on RSpec config, defaulting always to "spec/fixtures/files/"

RSpec.configure do |config|
...
  # This is no longer supported.
  config.fixture_path = "#{::Rails.root}/spec/support/fixtures"
..
end

Thanks @vipulnsward . I will have a look.

@vipulnsward why are you not using

config.file_fixture_path = "#{::Rails.root}/spec/custom_directory"

With

$ tree spec/ 
spec/
├── custom_directory
│   └── example.txt
require 'rails_helper'

RSpec.describe 'problem' do
  it 'change fixture path' do
    p fixture_file_upload("example.txt")
    expect(file_fixture("example.txt").read.chomp).to eq('YES')
  end
end

This pass. 🍏

Thanks @benoittgt file_fixture_path fixes it for us.

I will close this issue and encourage you to follow the last comments if you have any issues.

Was this page helpful?
0 / 5 - 0 ratings