Problem with Factory_girl, association and after_initialize

瘦欲@ 提交于 2019-12-03 04:44:54
Matteo Melani

This is the answer I got from Joe Ferris:

factory_girl doesn't pass arguments to the constructor. It uses #user= on your model, and instantiates it without any arguments.

and this one from Ben Hughes:

To elaborate on what Joe is saying, after_initialize methods are called immediately upon object initialization, and that time indeed user has not been set.

So for example while this will work:

family = Family.create!(:user => @user) # or @user.families.create ...

This will not (which is what factory_girl is doing under the hood):

family = Family.new
family.user = @user
family.save!

Just in general you want to be real careful using after_initialize, as remember this is called on every object initialization. A Family.all call on 1,000 objects will cause that to get called 1,000 times.

Sounds like in this instance you might be better of using a before_validation instead of after_initialize.

The following syntax also works for testing in rspec:

let (:family) { Family.create(:user => @user) }

Since after_initialize is triggered after new objects are instantiated and factory_girl builds instances by calling new without any arguments by default, you must use initialize_with to overwrite the default build.

FactoryGirl.define do
  factory :family do
    initialize_with { new(user: build(:user)) }
  end
end

I believe that it's because the association is lazy, thus in the "after_initialize" there's no user yet.

http://rdoc.info/github/thoughtbot/factory_girl/v1.3.3/file/README.rdoc

Perhaps you can directly call one factory from another, but I didn't try this, e.g.

f.user Factory(:user)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!