我如何在FactoryGirl中为循环关联创建工厂?

bru*_*077 6 ruby unit-testing ruby-on-rails associations factory-bot

我一直在努力为我在项目中定义的某些对象和关联创建工厂.我有一种循环的关联,其中一个对象与之后连接在一起的另外两个对象相关联.

+--------------+           +-------------+
|              |           |             |
| TestCase     +---------> | TestDataGrid|
|              |           |             |
+------+-------+           +------+------+
       |                          |
       |                          |
       |                          |
       v                          v
+--------------+           +--------------+
|              |           |              |
|              |           |              |
| TestVariable |           | TestDataSet  |
|              |           |              |
+------+-------+           +------+-------+
       |                          |
       |                          |
       |                          |
       |                          |
       |     +---------------+    |
       |     |               |    |
       |     |               |    |
       +---> | TestDataValue |<---+
             |               |
             +---------------+
Run Code Online (Sandbox Code Playgroud)
class TestCase < ActiveRecord::Base
  has_many :test_variables, dependent: :destroy
  has_many :test_data_grids
  #...omitted code...
end

class TestVariable < ActiveRecord::Base
  belongs_to :test_case
  has_many :test_data_values
  #...omitted code...
end

class TestDataValue < ActiveRecord::Base
  belongs_to :test_variable
  belongs_to :test_data_set
  #...omitted code...
end

class TestDataSet < ActiveRecord::Base
  belongs_to :test_data_grid
  has_many :test_data_values
  #...omitted code...
end

class TestDataGrid < ActiveRecord::Base
  belongs_to :test_case
  has_many :test_data_sets
  #...omitted code...
end
Run Code Online (Sandbox Code Playgroud)

基本上,关联在TestCase中拆分并再次在TestDataValue中连接,我怎样才能创建一个打开和关闭具有相同对象的圆的工厂?

ver*_*as1 0

这没有经过测试,但是:

FactoryGirl.define do

 factory :test_data_value do
  test_data_set
  test_variable
  # attributes
 end

 factory :test_data_set do
  test_data_grid
  # attributes
 end

 factory :test_variable do
  test_case
  # attributes
 end

 factory :test_case do
  # attributes
 end

 factory :test_data_grid do
  test_case
  # attributes
 end
end
Run Code Online (Sandbox Code Playgroud)

然后在规格中:

@test_data_value = FactoryGirl.create(:test_data_value)

@test_variable = @test_data_value.test_variable
@test_data_set = @test_data_value.test_data_set
Run Code Online (Sandbox Code Playgroud)