我正在尝试通过覆盖自定义 Django 模型属性factory_boy
以进行测试。但它似乎只是采用模型的默认行为。工厂男孩不能更改自定义属性的默认行为吗?
这是我写的一个基本测试:
模型.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
会话工厂.py:
class SessionFactory(factory.django.DjangoModelFactory):
class Meta:
model = Session
name = "session"
foo = 1337
测试.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