'Factory' class 的 FactoryGirl `factory define` 模式创建了 n 种 Class 类型
FactoryGirl `factory define` pattern for a 'Factory' class which creates n number of Class types
我将尽可能简短,但请假设即将完全重写我要描述的内容 not on this sprint
假设有一个工厂class喜欢(伪):
class AFactory
....
def self.distribute_types(args)
case args[:type]
when A
return ClassOfTypeA.new(args)
when B
return ClassOfTypeB.new(args)
when C
return ClassOfTypeC.new(args)
else
return ClassNamedSue(args)
end
end
end
我需要为此 "factory" class 编写 FactoryGirl 特征,但我找不到如何正确使用代表 class(es) 的 /other/ 工厂的示例待建,视情况而定。
edit: Please note, disregard the actual AFactory
pattern - it is
known as suspect and will be properly redistributed along healthy
guidelines later on. Please do not spend time considering it's
wonky-ness and instead, focus on how to properly test this -- each
generated class instance, has a factory already dedicated to it, and I
would like to utilize those in this AFactory
test.
如何将一个工厂包含在另一个工厂中 - 我想这是一个很糟糕的问题。
我认为为此 class 使用 FactoryGirl 没有任何价值。它唯一的方法是在给定正确参数的情况下生成 class 实例的方法。
更理想的方法是设置 N 个案例 - N 表示 args[:type]
可以采用的可能值的数量 - 并以此方式进行测试。当您向此 class 提供正确的类型时,您知道您期望什么,因此根本没有理由将 FactoryGirl 包含在此测试中。
这是一个粗略的 RSpec 骨架。除了你的工厂给了你你想要的类型这一事实之外,你没有测试任何东西(你还想测试它是否添加了你期望的值,但我把它留作 reader 的练习).
describe AFactory do
context 'when args[:type] is A' do
let(:args) {
{type: A}
}
it 'returns the right instance' do
result = AFactory.distribute_types(args)
# verify here
end
end
context 'when args[:type] is B' do
let(:args) {
{type: B}
}
it 'returns the right instance' do
result = AFactory.distribute_types(args)
# verify here
end
end
context 'when args[:type] is C' do
let(:args) {
{type: C}
}
it 'returns the right instance' do
result = AFactory.distribute_types(args)
# verify here
end
end
context 'when args[:type] is any other value' do
let(:args) {
{type: 'foo bar baz bing boof'}
}
it 'returns the right instance' do
result = AFactory.distribute_types(args)
# verify here
end
end
end
我将尽可能简短,但请假设即将完全重写我要描述的内容 not on this sprint
假设有一个工厂class喜欢(伪):
class AFactory
....
def self.distribute_types(args)
case args[:type]
when A
return ClassOfTypeA.new(args)
when B
return ClassOfTypeB.new(args)
when C
return ClassOfTypeC.new(args)
else
return ClassNamedSue(args)
end
end
end
我需要为此 "factory" class 编写 FactoryGirl 特征,但我找不到如何正确使用代表 class(es) 的 /other/ 工厂的示例待建,视情况而定。
edit: Please note, disregard the actual
AFactory
pattern - it is known as suspect and will be properly redistributed along healthy guidelines later on. Please do not spend time considering it's wonky-ness and instead, focus on how to properly test this -- each generated class instance, has a factory already dedicated to it, and I would like to utilize those in thisAFactory
test.
如何将一个工厂包含在另一个工厂中 - 我想这是一个很糟糕的问题。
我认为为此 class 使用 FactoryGirl 没有任何价值。它唯一的方法是在给定正确参数的情况下生成 class 实例的方法。
更理想的方法是设置 N 个案例 - N 表示 args[:type]
可以采用的可能值的数量 - 并以此方式进行测试。当您向此 class 提供正确的类型时,您知道您期望什么,因此根本没有理由将 FactoryGirl 包含在此测试中。
这是一个粗略的 RSpec 骨架。除了你的工厂给了你你想要的类型这一事实之外,你没有测试任何东西(你还想测试它是否添加了你期望的值,但我把它留作 reader 的练习).
describe AFactory do
context 'when args[:type] is A' do
let(:args) {
{type: A}
}
it 'returns the right instance' do
result = AFactory.distribute_types(args)
# verify here
end
end
context 'when args[:type] is B' do
let(:args) {
{type: B}
}
it 'returns the right instance' do
result = AFactory.distribute_types(args)
# verify here
end
end
context 'when args[:type] is C' do
let(:args) {
{type: C}
}
it 'returns the right instance' do
result = AFactory.distribute_types(args)
# verify here
end
end
context 'when args[:type] is any other value' do
let(:args) {
{type: 'foo bar baz bing boof'}
}
it 'returns the right instance' do
result = AFactory.distribute_types(args)
# verify here
end
end
end