【发布时间】:2012-09-01 12:10:26
【问题描述】:
我在尝试为我在项目中定义的一些对象和关联创建工厂时遇到了麻烦。我有一种循环类型的关联,其中一个对象与其他两个对象相关联,这些对象随后连接在一起。
+--------------+ +-------------+
| | | |
| 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中再次加入,我如何创建一个用相同对象打开和关闭圆的工厂?
【问题讨论】:
-
你真的需要那个吗?在大多数情况下,您可以模拟和存根所有这些关系。维护如此复杂的工厂非常困难。
标签: ruby-on-rails ruby unit-testing associations factory-bot