Unit testing 创建具有多个关联的factory对象
我有3个模型Unit testing 创建具有多个关联的factory对象,unit-testing,ruby-on-rails-4,rspec,factory-bot,Unit Testing,Ruby On Rails 4,Rspec,Factory Bot,我有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
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行中为第9行生成的account_type的值。我该怎么做呢我想您应该研究一下
瞬态
块和后的
回调
我在这个工厂里做了类似的事情,我获取了最近创建的事件会话的属性,并使用它们来创建和发布事件会话
希望这能帮助你,或者为你指明正确的方向
# 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