thoughtbot/factory_girl — Issue #259: after_destroy callback not called when record created by FactoryGirl
Author: jackrg
Created: 2011-12-20 20:31:26+00:00
State: open

I have the following code in my factories.rb file:

Factory.define :group do |group|
group.name { |n| "Group \##{n}}" }
end

Factory.define :brand, :parent => :group do |brand|
brand.type 'Brand'
end

If I use the following code in my RSpec test:

brand = Factory(:brand, :name => "Test")
brand.destroy

then the after_destroy callback I defined in the "brand" model is not called. If, instead, I use:

brand = Brand.create!(:name => "Test")
brand.destroy

then the after_destroy callback is called. I've verified that the record is created and deleted (brand.persisted?, Brand.count, etc. before and after the destroy), and I am verifying whether the after_destroy method is called by checking the side effect of the after_destroy and by setting a breakpoint in the after_destroy method.

Does anyone have an idea why this behavior exists?