创建具有多个关联的工厂对象
Creating a factory object with multiple association
我有 3 个模型
class Account
end
class AccountType
end
class Lookup
end
我想创建一个工厂
1 factory :account_test, class: 'Account' do
2 sequence(:name) {|i| "Account #{i}"}
3 association :account_type, factory: :account_type_with_data
4 association :lookup, factory: :lookup_value_with_account_type
5 active "1"
6 end
7
8 factory :lookup_value_with_account_type, class: 'Lookup' do
9 association :account_type, ????????
10 end
在第二个工厂的情况下,我想将工厂第 3 行中生成的 account_type 的值用于第 9 行。我该怎么做
我想你想查看 transient
块和 after
回调。
我在这个工厂中做了类似的事情,我在其中获取最近创建的 event_session
的属性并使用它们来创建和事件会话 post。
希望这对您有所帮助,或者可能为您指明正确的方向。
# Usage Notes:
# Use create(:event_session, create_post: true) to create associated event session post using event_session's title and body.
FactoryGirl.define do
factory :event_session do
transient do
event_session nil
create_post false
end
name {Faker::Commerce.product_name}
description { Faker::Company.catch_phrase + '. ' + Faker::Company.catch_phrase }
start_date_time { Time.now }
end_date_time { Time.now + 1.hours }
track_name { Faker::Company.catch_phrase }
session_type { Faker::Commerce.product_name }
room_name { Faker::Commerce.product_name }
sponsor { create(:sponsor) }
display_rank [*1..100].sample
event { create(:event, :random)}
sequence(:client_id) { |n| "client_id_#{n}" }
after(:create) do |event_session, evaluator|
create(:post, event_session: event_session, title: evaluator.name, body: evaluator.description) if @overrides.present? && @overrides[:create_post] || evaluator == true
end
end
end
我有 3 个模型
class Account
end
class AccountType
end
class Lookup
end
我想创建一个工厂
1 factory :account_test, class: 'Account' do
2 sequence(:name) {|i| "Account #{i}"}
3 association :account_type, factory: :account_type_with_data
4 association :lookup, factory: :lookup_value_with_account_type
5 active "1"
6 end
7
8 factory :lookup_value_with_account_type, class: 'Lookup' do
9 association :account_type, ????????
10 end
在第二个工厂的情况下,我想将工厂第 3 行中生成的 account_type 的值用于第 9 行。我该怎么做
我想你想查看 transient
块和 after
回调。
我在这个工厂中做了类似的事情,我在其中获取最近创建的 event_session
的属性并使用它们来创建和事件会话 post。
希望这对您有所帮助,或者可能为您指明正确的方向。
# Usage Notes:
# Use create(:event_session, create_post: true) to create associated event session post using event_session's title and body.
FactoryGirl.define do
factory :event_session do
transient do
event_session nil
create_post false
end
name {Faker::Commerce.product_name}
description { Faker::Company.catch_phrase + '. ' + Faker::Company.catch_phrase }
start_date_time { Time.now }
end_date_time { Time.now + 1.hours }
track_name { Faker::Company.catch_phrase }
session_type { Faker::Commerce.product_name }
room_name { Faker::Commerce.product_name }
sponsor { create(:sponsor) }
display_rank [*1..100].sample
event { create(:event, :random)}
sequence(:client_id) { |n| "client_id_#{n}" }
after(:create) do |event_session, evaluator|
create(:post, event_session: event_session, title: evaluator.name, body: evaluator.description) if @overrides.present? && @overrides[:create_post] || evaluator == true
end
end
end