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

Factory before(:create) called AFTER model after_create #1654

Closed cedvw closed 1 month ago

cedvw commented 1 month ago

Description

The factory's before(:create) callback is called AFTER the model's after_create.

Reproduction Steps

Given a simple model, let's call it Account, with an after_create callback:

class Account < ApplicationRecord
  after_create :do_something

  def do_something
    puts 'Model after_create callback'
  end
end

And a factory for it:

FactoryBot.define do
  factory :account do
    before(:create) do
      puts 'Factory before_create callback'
    end
  end
end

When running a test to create an account using the above factory, the following is printed:

Model after_create callback
Factory before_create callback

Expected behavior

The order of the callbacks should be before_create followed by after_create.

Actual behavior

Callbacks are called out of order.

System configuration

factory_bot version: 5.2 rails version: 5.0 ruby version: 2.4

cedvw commented 1 month ago

Turned out to be a bug in my application. Closing.