4

我决定在我的简单 django 应用程序中使用 factory_boy 进行测试。但是我对简单的例子有疑问。这是我的简单测试的代码。

from django.utils import unittest
from ..models import Server, ServerAddress, L2TPServer, serialize_open_vpn_server_json

from factory import django as django_factory


class SshOpenVpnServerFactory(django_factory.DjangoModelFactory):
    class Meta:
        model = L2TPServer
        django_get_or_create = ('name', 'address')

    name = 'Hello'
    address = 'Nono'


class ServersTestCase(unittest.TestCase):

    def test_serialize_server_info(self):
        print Server.objects.all()
        SshOpenVpnServerFactory.build()

当 test-runner 执行这个测试时,我得到一个错误:

Traceback (most recent call last):
  File "/Users/green/Development/Wasel/experimental/wasel_services/packages/waselcore/backend/tests/test_models.py", line 20, in test_serialize_server_info
    SshOpenVpnServerFactory.build()
  File "/Users/green/Development/Wasel/experimental/env/wasel_sevices/lib/python2.7/site-packages/factory/base.py", line 504, in build
    attrs = cls.attributes(create=False, extra=kwargs)
  File "/Users/green/Development/Wasel/experimental/env/wasel_sevices/lib/python2.7/site-packages/factory/base.py", line 365, in attributes
    force_sequence=force_sequence,
  File "/Users/green/Development/Wasel/experimental/env/wasel_sevices/lib/python2.7/site-packages/factory/containers.py", line 265, in build
    sequence = self.factory._generate_next_sequence()
  File "/Users/green/Development/Wasel/experimental/env/wasel_sevices/lib/python2.7/site-packages/factory/base.py", line 338, in _generate_next_sequence
    cls._setup_counter()
  File "/Users/green/Development/Wasel/experimental/env/wasel_sevices/lib/python2.7/site-packages/factory/base.py", line 318, in _setup_counter
    first_seq = cls._setup_next_sequence()
  File "/Users/green/Development/Wasel/experimental/env/wasel_sevices/lib/python2.7/site-packages/factory/django.py", line 83, in _setup_next_sequence
    manager = cls._get_manager(model)
  File "/Users/green/Development/Wasel/experimental/env/wasel_sevices/lib/python2.7/site-packages/factory/django.py", line 76, in _get_manager
    return target_class.objects
AttributeError: 'NoneType' object has no attribute 'objects'

我哪里错了?factory_boy 支持 django 1.6.5 吗?

4

1 回答 1

4

使用Meta该类的建议语法将仅作为(未发布的)Factory Boy 2.4 的一部分发布。改为使用SshOpenVpnServerFactory.FACTORY_FOR。相关问题:https ://github.com/rbarrois/factory_boy/issues/143

于 2014-06-01T10:12:17.610 回答