Why is fixture_file_upload not available in RSpec (without ActiveRecord)?
Asked Answered
S

2

8

I'm trying to test a file upload using RSpec 3.7 in a Rails 5.2 app, and the simplest recommendation I've seen (several places, including this SO post) is to use fixture_file_upload - which looks great, except that it doesn't seem to be available in my app, and I don't know why.

The only thing I can think of is that our app is not using ActiveRecord, it's using MongoID. But ActiveRecord shouldn't be required to use these methods... they're totally unrelated. Is there another new "ActiveSomething" library in Rails 5 that I'm missing? (this app was upgraded from a Rails 4 app...)

To explain my problem more concretely, I've tried putting:

let(:file) { fixture_file_upload('invalid_csv.csv') }

in one of my contexts, and it raises an exception:

undefined local variable or method 'fixture_path' for #<RSpec::ExampleGroups::...>

I tried defining config.file_fixture_path as outlined here, and that raises it's own exception:

undefined method `file_fixture_path=' for #<RSpec::Core::Configuration::...>

Does this work at all? Clearly I'm missing something...

Stamford answered 31/1, 2019 at 21:22 Comment(6)
How about change config.fixture_path = "#{Rails.root}/xxx/xxx" in RSpec.configure?Browder
I tried that as well, and I got the same error "undefined method for fixture_path= for <RSpec::Core::Configuration...>"Stamford
Did you ever figure this out? I ended up writing my own fixture_file_upload helper that was just Rack::Test::UploadedFile.new(Rails.root.join("spec/fixtures/#{path}"), mime, true). Seems like a ridiculous thing to do but it works in Rails6 and lets me get on with the upgrade so...Hensley
I don't think I figured this out, no. I think I just used workarounds like that. However, it appears it should be part of ActionDispatch::TestProcess::FixtureFile in Rails 6: apidock.com/rails/ActionDispatch/TestProcess/FixtureFile/…Stamford
I am having the same problem since I have upgraded from Rails 6.0 to Rails 6.1. By using the Rack::Test::UploadedFile.new(Rails.root.join("spec/fixtures/#{path}"), mime, true) as mentioned above has worked around the problem to me.Centuplicate
If someone still having this problem currently can test the proposed answer below, and let me know, I'll mark it as correct! :)Stamford
F
2

I've fixed this by including their module on the RSpec config block:

RSpec.configure do |config|
  #...
  config.include ActionDispatch::TestProcess::FixtureFile
  #...
end
Frederickfredericka answered 14/1, 2021 at 16:25 Comment(2)
Nice! I can't test right now, but if someone else can confirm that it works for them as well, I'll mark this as correct :)Stamford
Didn't work for me, upgraded from 6.0 to 6.1Elke
T
1

this worked for me

Rack::Test::UploadedFile.new(Rails.root.join("spec/fixtures/doc.pdf"))
Toxoplasmosis answered 27/8, 2021 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.