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...

Javier Menéndez Rizo
  • 2,138
  • 3
  • 12
  • 22
mltsy
  • 6,598
  • 3
  • 38
  • 51
  • 1
    How about change `config.fixture_path = "#{Rails.root}/xxx/xxx"` in RSpec.configure? – mogbee Feb 02 '19 at 03:14
  • 1
    I tried that as well, and I got the same error "undefined method for `fixture_path=` for " – mltsy Feb 04 '19 at 18:43
  • 2
    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... – mu is too short Dec 30 '20 at 06:41
  • 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: https://apidock.com/rails/ActionDispatch/TestProcess/FixtureFile/fixture_file_upload – mltsy Jan 13 '21 at 18:34
  • 1
    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. – evedovelli Jan 14 '21 at 02:11
  • If someone still having this problem currently can test the proposed answer below, and let me know, I'll mark it as correct! :) – mltsy Feb 18 '21 at 18:11

2 Answers2

2

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

RSpec.configure do |config|
  #...
  config.include ActionDispatch::TestProcess::FixtureFile
  #...
end
sequielo
  • 1,541
  • 1
  • 18
  • 27
1

this worked for me

Rack::Test::UploadedFile.new(Rails.root.join("spec/fixtures/doc.pdf"))
Thomas Van Holder
  • 1,137
  • 9
  • 12