简体   繁体   中英

Failure/Error FactoryGirl.create

I'm having trouble making multiple factories of the same class in factorygirl.

I've got 4 models

  • aaa has bbbs & cccs
  • bbb belongs to aaa & has ddds
  • ccc belongs to aaa & has ddds
  • ddd belongs to bbb & ccc

Here are my factoies

aaas.rb

FactoryGirl.define do
  factory :aaa do
    after(:create) do |aaa|
      create_list(:bbb_a, 1, aaa: aaa)
      create_list(:ccc_a, 1, aaa: aaa)
    end
  end
end

bbbs.rb

FactoryGirl.define do
  factory :bbb do
    factory :bbb_a do
       ddd_a
    end
  end
end

cccs.rb

FactoryGirl.define do
  factory :ccc do
    factory :ccc_a do
      ddd_a
    end
  end
end

ddds.rb

FactoryGirl.define do
  factory :ddd do
    factory :ddd_a do
    end
  end
end

Here's the test I'm running to get the error

aaa_spec.rb

require 'spec_helper'
describe Aaa do
  it "works" do
    aaa = FactoryGirl.create(:aaa)
    puts aaa
    puts aaa.bbbs 
    puts aaa.cccs
    aaa.bbbs.each {|bbb| puts bbb.ddd}
    aaa.cccs.each {|ccc| puts ccc.ddd}
  end
end

I had bbb_b, bbb_c, ccc_b, ccc_c, ddd_b, and ddd_c in there too, but they are not needed to get the error.

aaas.rb

FactoryGirl.define do
  factory :aaa do
    after(:build) do |aaa|
      aaa.bbbs << build(:bbb_a)
      aaa.cccs << build(:ccc_a)
    end
  end
end

bbbs.rb

FactoryGirl.define do
  factory :bbb do
    factory :bbb_a do
      after(:build) do |bbb|
        bbb.ddd = ddd_a
      end
    end
  end
end

cccs.rb

FactoryGirl.define do
  factory :ccc do
    factory :ccc_a do
      after(:build) do |ccc|
        ccc.ddd = ddd_a
      end
    end
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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