失败/错误FactoryGirl.create

时间:2014-04-25 16:34:42

标签: ruby-on-rails ruby rspec factory-bot

我在工厂里制造同一个班级的多个工厂时遇到了麻烦。

我有4个模特

  • aaa有bbbs& CCCS
  • bbb属于aaa&有ddds
  • ccc属于aaa&有ddds
  • ddd属于bbb& CCC

以下是我的事实

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

这是我正在运行以获取错误的测试

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

我也有bbb_b,bbb_c,ccc_b,ccc_c,ddd_b和ddd_c,但不需要它们来获取错误。

1 个答案:

答案 0 :(得分:0)

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