作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 Django 项目中编写单元测试。我有一个工厂可以使用 .create()
创建对象方法。所以在我的单元测试中,我使用了这个:
device = DeviceFactory.create()
最佳答案
报价 this bit of the documentation , 使用 .build()
而不是 .create()
:
# Returns a User instance that's not saved
user = UserFactory.build()
# Returns a saved User instance.
# UserFactory must subclass an ORM base class, such as DjangoModelFactory.
user = UserFactory.create()
# Returns a stub object (just a bunch of attributes)
obj = UserFactory.stub()
# You can use the Factory class as a shortcut for the default build strategy:
# Same as UserFactory.create()
user = UserFactory()
关于python - 如何让 DjangoModelFactory 创建模型而不将其保存到数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58696893/
我是一名优秀的程序员,十分优秀!