thoughtbot / factory_bot

A library for setting up Ruby objects as test data.
https://thoughtbot.com
MIT License
7.89k stars 2.6k forks source link

Support for callback before(:build) #1633

Open 23tux opened 3 months ago

23tux commented 3 months ago

Problem this feature will solve

I want to disable our Bullet gem when FactoryBot is building instances and creating records, because I don't care about n+1 problems inside my test setup.

The problem is, that there seems to be no callback called before(:build). I had a look into the source code but didn't find the right place to patch it.

Any help is appreciated where to start, so I maybe I can try for a PR.

Desired solution

I want to be able to use something like this

FactoryBot.define do
  before(:build) do |record|
    Bullet.enable = false
  end

  after(:create) do |record|
    Bullet.enable = true
  end
end
ofabianomartins commented 3 months ago

I'm interested too!

mohammednasser-32 commented 2 months ago

Created a PR for the change https://github.com/thoughtbot/factory_bot/pull/1639