简体   繁体   English

具有多个特征的工厂女工协会

[英]Factory girl association with more than one trait

I have a association like this : 我有这样的关联:

association :address, :factory => [:address, :closer_address]

Where my factory is like this: 我的工厂是这样的:

factory :address do
  address1 "12 Any Street"
  latitude 22.4583397
  longitude -11.06776
  state 'pending_verification'

  trait :closer_address do
    latitude 33.4783397
    longitude -11.06776
  end

  trait :verified do
    state 'verified'
  end
end

So can I somehow create an association with more than one trait? 那么我可以以某种方式创建具有多个特征的关联吗? Or there is another way around it? 还是有另一种解决方法? The idea is that I want to have closer_address which is also verified, and in another case I might want closer_address which is not verified, so that's why keeping them separate. 我的想法是,我希望具有也已验证的closer_address ,在另一种情况下,我可能想要未验证的closer_address ,因此这是将它们分开的原因。

Any ideas? 有任何想法吗?

You could do this to have two different trait options: 您可以这样做以具有两个不同的特征选项:

  factory :address do
    address1 "12 Any Street"
    latitude 22.4583397
    longitude -11.06776
    state 'pending_verification'

    trait :closer_address do
      latitude 33.4783397
      longitude -11.06776
    end

    trait :verified_closer_address do
      latitude 33.4783397
      longitude -11.06776
      state 'verified'
    end
  end

You would create the objects like this: 您将创建如下对象:

:factory => [:address, :closer_address]

or this: 或这个:

:factory => [:address, :verified_closer_address] 

Or you could do this without changing your current factory: 或者您可以在不更改当前工厂的情况下执行此操作:

  factory :address do
    address1 "12 Any Street"
    latitude 22.4583397
    longitude -11.06776
    state 'pending_verification'

    trait :closer_address do
      latitude 33.4783397
      longitude -11.06776
    end

    trait :verified do
      state 'verified'
    end
  end

You would create the objects like this: 您将创建如下对象:

:factory => [:address, :closer_address, :verified]

It is possible to use multiple traits simultaneously when creating an object with FactoryGirl. 使用FactoryGirl创建对象时,可以同时使用多个特征。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM