Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 创建具有多个关联的factory对象_Unit Testing_Ruby On Rails 4_Rspec_Factory Bot - Fatal编程技术网

Unit testing 创建具有多个关联的factory对象

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

我有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行中为第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