如何在多个文件中拥有一个工厂
How to have a factory in multiple files
我有一个模型被多个库使用,每个库都以不同的方法使用它。
目前我制作了一个工厂文件,其中包含多个子工厂。我的每个库都有一个工厂,因为每个库都需要我的基本模型的不同配置。
我遇到的问题是我的工厂文件太长,超过 400 行,我想将它分成多个文件,每个库一个。
现在我有这样的东西
# Master file
FactoryBot.define do
factory :my_model do
# Some things
factory :lib_1_test_unit do
# Some other things
end
factory :lib_2_test_unit do
# Some other things
end
end
end
我试着这样定义它:
# New master file
FactoryBot.define do
factory :my_model do
# Some things
end
end
# Subfile 1
FactoryBot.define do
factory :lib_1_test_unit do
# Some other things
end
end
但是我得到了那个错误:
uninitialized constant Lib1TestUnit
如果我用工厂包装我的子文件 1 :my_model,我得到另一个错误,因为我不能定义它两次。
我也尝试将子文件包含到主文件中,但效果更差
有没有办法实现我想做的事情?或者我走错了路,也许有更好的做法或其他方法可以将同一对象测试到不同的库中?
这不是您问题的完整解决方案,更像是评论。
也许您错过了子文件 1 中的 "do"。
你有:
factory :lib_1_test_unit
你需要:
factory :lib_1_test_unit do
似乎没有相应的功能,也不需要
我有一个模型被多个库使用,每个库都以不同的方法使用它。
目前我制作了一个工厂文件,其中包含多个子工厂。我的每个库都有一个工厂,因为每个库都需要我的基本模型的不同配置。
我遇到的问题是我的工厂文件太长,超过 400 行,我想将它分成多个文件,每个库一个。
现在我有这样的东西
# Master file
FactoryBot.define do
factory :my_model do
# Some things
factory :lib_1_test_unit do
# Some other things
end
factory :lib_2_test_unit do
# Some other things
end
end
end
我试着这样定义它:
# New master file
FactoryBot.define do
factory :my_model do
# Some things
end
end
# Subfile 1
FactoryBot.define do
factory :lib_1_test_unit do
# Some other things
end
end
但是我得到了那个错误:
uninitialized constant Lib1TestUnit
如果我用工厂包装我的子文件 1 :my_model,我得到另一个错误,因为我不能定义它两次。
我也尝试将子文件包含到主文件中,但效果更差
有没有办法实现我想做的事情?或者我走错了路,也许有更好的做法或其他方法可以将同一对象测试到不同的库中?
这不是您问题的完整解决方案,更像是评论。
也许您错过了子文件 1 中的 "do"。
你有:
factory :lib_1_test_unit
你需要:
factory :lib_1_test_unit do
似乎没有相应的功能,也不需要