How to avoid circular creation of associated models in factory_girl?

后端 未结 2 756
南旧
南旧 2021-02-14 00:10

I have an app where a user can sign in with multiple services, e.g. Google Plus, Facebook, Twitter, etc.

To facilitate this, I have a base User model which

2条回答
  •  别跟我提以往
    2021-02-14 00:27

    I don't think there's a nice way for a factory to tell that it's been called by another without collaboration. (You can always inspect caller_locations, but that's not nice.) Instead, have one factory tell the other to behave differently using a transient attribute:

    FactoryGirl.define do
      factory :user do
        transient do
          create_identity true
        end
    
        after(:create) do |user, evaluator|
          if evaluator.create_identity
            create(:identity, user: user)
          end
        end
    
      end
    
      factory :identity do
        association :user, factory: :user, create_identity: false
      end
    
    end
    

提交回复
热议问题