Skip to content

Fixture_file_upload problems #385

@davidpatrick

Description

@davidpatrick

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?

Activity

jferris

jferris commented on May 22, 2012

@jferris
Contributor

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

davidpatrick

davidpatrick commented on May 23, 2012

@davidpatrick
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

zerodie commented on Jun 7, 2012

@zerodie

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

ferdinandrosario

ferdinandrosario commented on Apr 9, 2014

@ferdinandrosario

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

sqbell

sqbell commented on Jun 4, 2014

@sqbell

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

pjg commented on Nov 15, 2018

@pjg
Contributor

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

josephbhunt commented on Feb 13, 2019

@josephbhunt

@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

pjg commented on Feb 14, 2019

@pjg
Contributor

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

added a commit that references this issue on Jul 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jferris@pjg@josephbhunt@zerodie@sqbell

        Issue actions

          Fixture_file_upload problems · Issue #385 · thoughtbot/factory_bot