gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 07:08:33 25 4
gpt4 key购买 nike

我在尝试为我在项目中定义的某些对象和关联创建工厂时遇到了问题。我有一种循环关联,其中一个对象与另外两个随后连接在一起的对象关联。

+--------------+           +-------------+
| | | |
| TestCase +---------> | TestDataGrid|
| | | |
+------+-------+ +------+------+
| |
| |
| |
v v
+--------------+ +--------------+
| | | |
| | | |
| TestVariable | | TestDataSet |
| | | |
+------+-------+ +------+-------+
| |
| |
| |
| |
| +---------------+ |
| | | |
| | | |
+---> | TestDataValue |<---+
| |
+---------------+

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

基本上关联在 TestCase 中拆分并在 TestDataValue 中再次加入,我如何创建一个工厂来打开和关闭具有相同对象的圆圈?

最佳答案

这没有经过测试,但是:

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

然后在规范中:

@test_data_value = FactoryGirl.create(:test_data_value)

@test_variable = @test_data_value.test_variable
@test_data_set = @test_data_value.test_data_set

关于ruby-on-rails - 我如何在 FactoryGirl 中为循环关联创建工厂?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12320984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com