【发布时间】:2016-05-20 07:47:11
【问题描述】:
出于测试目的,我正在尝试通过 factory_boy 覆盖自定义 Django 模型属性。但它似乎只是采用模型的默认行为。工厂男孩不能改变自定义属性的默认行为吗?
这是我写的一个基本测试:
models.py:
class Session(models.Model):
name = models.CharField(max_length=100)
@property
def foo(self):
return method_not_callable_in_testing()
def method_not_callable_in_testing():
return 42
SessionFactory.py:
class SessionFactory(factory.django.DjangoModelFactory):
class Meta:
model = Session
name = "session"
foo = 1337
tests.py: 类TestSession(TestCase):
def test_custom_attribute_overwritten_by_factoryboy(self):
session = SessionFactory.create()
self.assertEquals(session.foo, 1337)
运行测试时出现以下错误:
F
======================================================================
FAIL: test_custom_attribute_overwritten_by_factoryboy (bar.tests.TestSession)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/sh4ke/repos/foo/bar/tests.py", line 10, in test_custom_attribute_overwritten_by_factoryboy
self.assertEquals(session.foo, 1337)
AssertionError: 42 != 1337
【问题讨论】:
标签: python django factory-boy