Coder Social home page Coder Social logo

Comments (5)

composerinteralia avatar composerinteralia commented on June 18, 2024

Thanks for the issue. I was able to reproduce this. It has to do with the fact that you are creating a Rack::Test::UploadedFile.new in the FactoryBot.define block. There seems to be a reference to a tempfile that no longer exists. I will look into this further, but in the meantime it looks like you can work around the issue by creating the Rack::Test::UploadedFile in the attribute block:

image_file = Rails.root.join("spec", "support", "fixtures", "images", "image1.jpeg")

factory :user do
  profile_image { Rack::Test::UploadedFile.new(image_file, "image/jpeg") }
end

from factory_bot_rails.

maestromac avatar maestromac commented on June 18, 2024

Thanks! I'll go ahead and do that for now.

from factory_bot_rails.

composerinteralia avatar composerinteralia commented on June 18, 2024

Some more details here:

  1. On the first test run, spring starts up and the application initializes
  2. factory_bot loads and this factory definition instantiates a Rack::Test::UploadedFile
  3. Rack::Test::UploadedFile creates a tempfile, and an ObjectSpace finalizer to clean up the temp file
  4. The test runs successfully
  5. The finalizer runs and cleans up the temp file. This is surprising to me, since factory_bot is still holding a reference to the object.
  6. On the second test run, the application is preloaded. (If any files have changed spring will reload the application and we won't see the error)
  7. factory_bot is already loaded, and holds a reference to the Rack::Test::UploadedFile, but its tempfile has already been cleaned up
  8. Tests fail as described in this PR

We could solve this by adding back the logic to reload factory_bot after every spring fork, but I don't love the idea of adding references to spring back into factory_bot (we removed that in 02a0f58).

I think the first step is to dig a little more into why that rack finalizer is running when it does.

from factory_bot_rails.

composerinteralia avatar composerinteralia commented on June 18, 2024

Aha, this has to do with the fact that spring runs in a forked process. Consider this:

ObjectSpace.define_finalizer({}, proc { puts "Finalized" }) 

fork

Running this code you will see "Finalized" printed twice, once for the original process and once for the forked process.

So the tempfile gets deleted by the finalizer in the spring forked process, but we still hold a reference to the Rack::Test::UploadedFile in the main process, and in any later spring forks.

from factory_bot_rails.

composerinteralia avatar composerinteralia commented on June 18, 2024

I am going to close this issue without fixing for now. There is a reasonable workaround, and we also have https://github.com/thoughtbot/factory_bot/issues to improve the file upload experience in factory_bot.

More generally, factory_bot does not currently have great support for using non-factory_bot code to help in building object instances. We are tracking this in thoughtbot/factory_bot#1252.

from factory_bot_rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.