Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixture_file_upload problems #385

Closed
davidpatrick opened this issue May 22, 2012 · 8 comments
Closed

Fixture_file_upload problems #385

davidpatrick opened this issue May 22, 2012 · 8 comments

Comments

@davidpatrick
Copy link

The other day I was having no problems running fixture_file_upload in my factory. Previously I just used a basic Factory.define but I wanted to get rid of the deprecated warnings.

So today I went to run my tests and I am getting the following errors:

undefined method 'fixture_file_upload' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2:0x007fe6822f6e10>
undefined method `fixture_file_upload' for #<FactoryGirl::SyntaxRunner:0x007fe6827c2188>

The code looks like this

FactoryGirl.define do
    factory :mongo_document do 
      original_filename "test.jpg"
      file {fixture_file_upload(Rails.root.to_s + '/spec/fixtures/files/test.jpg', 'img/jpeg')}
  end
end

I have even tried putting in

spec_helper / test_helper

include ActionController::TestProcess

No luck. What am I missing?

@jferris
Copy link
Member

jferris commented May 22, 2012

Where is fixture_file_upload defined? Can you try Kernel.fixture_file_upload?

@davidpatrick
Copy link
Author

Okay so it looks like my project isn't loading the test_helper.rb because when I change the code to the following it will work

include ActionDispatch::TestProcess
FactoryGirl.define do
    factory :mongo_document do 
      original_filename "test.jpg"
      file { fixture_file_upload(Rails.root.to_s + '/spec/fixtures/files/test.jpg', 'img/jpeg') }
  end
end

@zerodie
Copy link

zerodie commented Jun 7, 2012

Thank you @batreyud, it works for me adding "include ActionDispatch::TestProcess".

@ferdinandrosario
Copy link

Thanks for solution.It helps me also for "fixture_file_upload" in Rspec model.

@sqbell
Copy link

sqbell commented Jun 4, 2014

Be careful, I've had some rather obscure, session related problems when I included the module. See: http://stackoverflow.com/questions/18202261/include-actiondispatchtestprocess-prevents-guard-from-reloading-properly.

@pjg
Copy link
Contributor

pjg commented Nov 15, 2018

Don't include ActionDispatch::TestProcess. It has some nasty consequences.

Proper solution is to use Rack::Test::UploadedFile directly in the factory (what fixture_file_upload essentially does anyway):

file { Rack::Test::UploadedFile.new('spec/factories/test.png', 'image/png') }

@josephbhunt
Copy link

@pjg I've been trying to use Rack::Test::UploadedFile, but I get an error REXML::UndefinedNamespaceException: Undefined prefix Rack::Test: found. Do I need to require a file or include a module to make this work?

@pjg
Copy link
Contributor

pjg commented Feb 14, 2019

@josephbhunt Judging by the REXML::UndefinedNamespaceException exception it looks like you're in the wrong context when you execute this file helper (do you have it in the factory definition?). As a workaround you might try to prefix Rack with :: so that it references top-level namespace and becomes ::Rack::Test::UploadedFile.

kemenaran added a commit to demarches-simplifiees/demarches-simplifiees.fr that referenced this issue Jul 1, 2020
This is mostly for consistency (both styles were used before).

Note: we still have to use `Rack::Test::UploadedFile.new` in factories,
because of thoughtbot/factory_bot#385.
bors bot added a commit to demarches-simplifiees/demarches-simplifiees.fr that referenced this issue Jul 1, 2020
5318: specs: use `fixture_file_upload` rather than `Rack::Test::UploadedFile` r=Keirua a=kemenaran

This is mostly for consistency (both styles were used before).

Note: we still have to use `Rack::Test::UploadedFile.new` in factories,
because of thoughtbot/factory_bot#385.

Co-authored-by: Pierre de La Morinerie <kemenaran@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants