使用factory_girl命名蓝图
我正在使用 Factory Girl,但喜欢机械师语法。所以我想知道,是否有任何方法为类创建一个命名蓝图,以便我可以拥有类似的东西:
User.blueprint(:no_discount_user) do
admin false
hashed_password "226bc1eca359a09f5f1b96e26efeb4bb1aeae383"
is_trader false
name "foolish"
salt "21746899800.223524289203464"
end
User.blueprint(:discount_user) do
admin false
hashed_password "226bc1eca359a09f5f1b96e26efeb4bb1aeae383"
is_trader true
name "deadbeef"
salt "21746899800.223524289203464"
discount_rate { DiscountRate.make(:rate => 20.00) }
end
DiscountRate.blueprint do
rate {10}
not_before ...
not_after ...
end
有没有一种方法可以使用机械师语法来制作factory_girl?我没有找到。帮助表示赞赏。
提前谢谢 贾森
I am using Factory Girl but like the machinist syntax. So I wonder, if there is any way creating a named blueprint for class, so that I can have something like that:
User.blueprint(:no_discount_user) do
admin false
hashed_password "226bc1eca359a09f5f1b96e26efeb4bb1aeae383"
is_trader false
name "foolish"
salt "21746899800.223524289203464"
end
User.blueprint(:discount_user) do
admin false
hashed_password "226bc1eca359a09f5f1b96e26efeb4bb1aeae383"
is_trader true
name "deadbeef"
salt "21746899800.223524289203464"
discount_rate { DiscountRate.make(:rate => 20.00) }
end
DiscountRate.blueprint do
rate {10}
not_before ...
not_after ...
end
Is there a way making factory_girl with machinist syntax acting like that? I did not find one. Help appreciated.
Thx in advance
Jason
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你可以。需要蓝图语法
Yes you can. require blueprint syntaxe
如果您担心测试的干燥性,您可以考虑我创建的active_factory 插件。在其中,您可以像这样定义一个工厂:
另一个选项是在测试中“添加折扣”:
它将在两个模型之间创建关联。因此,您可以保持规格干燥,同时避免创建大量工厂。
抱歉,如果我没有准确回答您的问题
If you concerned with DRYness of your tests, you may consider active_factory plugin, that i've created. In it you could define a factory like this:
Another option would be just 'add discount' inside your test:
It would create association between the two models. Thus you can keep your specs DRY while avoiding creating a lot of factories.
Sorry, if I'm not answering exactly your question