Multiple versions of the same factory (FactoryGirl)

Is there a way to have multiple versions of the same factory? For example, userfactory.

FactoryGirl.define do
  factory :user#1 do
    name 'John Doe'
    date_of_birth { 21.years.ago }
  end

  factory :user#2 do
    name 'Jane Doe'
    date_of_birth { 25.years.ago }
  end
end

is there something like this that I can call FactoryGirl.create :user#1for John or FactoryGirl.create :user#2for Jane?

userfactory is an example that I don't actually use, but my real factory uses a lot of data. I find it cumbersome to manually change a lot of data every time I need a different user.

+4
source share
3 answers

You do not need to declare twice

just follow

FactoryGirl.define do
  factory :user do
    name
    date_of_bith
  end
end

Now you can call dynamically

user1 = FactoryGirl.create(:user, name: 'John doe', date_of_birth: 21.year.ago)
user2 = FactoryGirl.create(:user, name: 'Jane doe', date_of_birth: 25.year.ago) 
+2
source

, , , .

FactoryGirl.define do
  factory :mail_user do
    mail_domain
    account
    sequence :userid do |n|
        "mailuser-#{n}"
    end
    passwd "This!sADecentPassword11212"
    active :true
    filter :false
  end
end

/, ,

FactoryGirl.define do
  factory :administrator do
    isp
    sequence :email do |n|
      "user#{n}@example.com"
    end
    active true
    name "Random Awesome Admin"
    password '123Abc#$1'
    password_confirmation '123Abc#$1'

    trait :inactive do
      active false
    end

    trait :system do
      roles [:system_admin]
    end

    trait :admin do
      roles [:admin ]
    end

    trait :realm do
      roles [:realm_admin]
    end

    trait :helpdesk do
      roles [ :helpdesk ]
    end

    trait :dns do
      roles [ :dns_admin ]
    end

    factory :inactive_administrator, traits: [:inactive]
    factory :helpdesk_admin, traits: [:helpdesk]
    factory :system_admin, traits: [:system]
    factory :admin, traits: [:admin]
    factory :realm_admin, traits: [:realm]
    factory :dns_admin, traits: [:dns]
  end
end

. . //. .

0

, sequence

factory :user do
  sequence(:name) { |n| "person#{n}" }
  sequence(:date_of_birth) { |n| "#{n}".years.ago }
end

, ,

sequence(:date_of_birth, 15) { |n| "#{n}".years.ago }

0

Source: https://habr.com/ru/post/1534252/


All Articles