我正在嘗試使用factory_boy來幫助爲我的測試生成一些MongoEngine文檔。我在定義EmbeddedDocumentField
對象時遇到問題。如何使用factory_boy模擬MongoEngine EmbeddedDocument?
這裏是我的MongoEngine Document
:
class Comment(EmbeddedDocument):
content = StringField()
name = StringField(max_length=120)
class Post(Document):
title = StringField(required=True)
tags = ListField(StringField(), required=True)
comments = ListField(EmbeddedDocumentField(Comment))
這裏是我的一部分factory_boy Factory
完成:
class CommentFactory(factory.Factory):
FACTORY_FOR = Comment
content = "Platinum coins worth a trillion dollars are great"
name = "John Doe"
class BlogFactory(factory.Factory):
FACTORY_FOR = Blog
title = "On Using MongoEngine with factory_boy"
tags = ['python', 'mongoengine', 'factory-boy', 'django']
comments = [factory.SubFactory(CommentFactory)] # this doesn't work
任何想法如何指定comments
場?問題在於工廠男孩試圖創建嵌入式文檔Comment
。
當初始化主工廠時,您需要包含EmbeddedDocumentFactory,因爲第一個人已經包含爲嵌入式文檔工廠創建工廠。 –