Factory_bot: Fixture_file_upload problems

Created on 22 May 2012  路  8Comments  路  Source: thoughtbot/factory_bot

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?

Most helpful comment

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') }

All 8 comments

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

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

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

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

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.

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') }

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

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

Was this page helpful?
0 / 5 - 0 ratings