Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 326
mltsy Avatar asked Jan 31 '19 21:01

mltsy


2 Answers

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

RSpec.configure do |config|
  #...
  config.include ActionDispatch::TestProcess::FixtureFile
  #...
end
like image 91
sequielo Avatar answered Nov 09 '22 23:11

sequielo


this worked for me

Rack::Test::UploadedFile.new(Rails.root.join("spec/fixtures/doc.pdf"))
like image 40
Thomas Van Holder Avatar answered Nov 09 '22 22:11

Thomas Van Holder