混合器:创建实例时属性错误

2024-06-28 20:32:55 发布

您现在位置:Python中文网/ 问答频道 /正文

对于TDD,我使用:

pytest==3.7.2 pytest-django==3.4.1 mixer==6.0.1

但问题是我不能通过mixer创建任何实例。他们显示了一个属性错误:

AttributeError: 'Options' object has no attribute 'private_fields'

我的测试_型号.py以及型号.py代码:

#test_models.py
import pytest
from mixer.backend.django import mixer
from birdie.models import Post

@pytest.mark.django_db
class TestPost:

    def test_model(self):
        obj = mixer.blend(User) 
        assert obj.pk == 1, 'Should create a post instance'


# models.py

class Post(models.Model):
    body = models.TextField()

完全回溯错误:

================================================== FAILURES ======================================================================
_____________________________________ TestPost.test_model _________________________________________________________________

self = <birdie.tests.test_models.TestPost object at 0x7f719ab99898>

    def test_model(self):

>       obj = mixer.blend(User)

birdie/tests/test_models.py:22: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../../../.local/share/virtualenvs/tdd_test-D7r9ITf0/lib/python3.5/site-packages/mixer/main.py:566: in blend
    type_mixer = self.get_typemixer(scheme)
../../../.local/share/virtualenvs/tdd_test-D7r9ITf0/lib/python3.5/site-packages/mixer/main.py:585: in get_typemixer
    fake=self.params.get('fake'), factory=self.__factory)
../../../.local/share/virtualenvs/tdd_test-D7r9ITf0/lib/python3.5/site-packages/mixer/main.py:55: in __call__
    cls_type, mixer=mixer, factory=factory, fake=fake)
../../../.local/share/virtualenvs/tdd_test-D7r9ITf0/lib/python3.5/site-packages/mixer/main.py:88: in __init__
    self.__fields = _.OrderedDict(self.__load_fields())
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <TypeMixer <class 'django.contrib.auth.models.User'>>

    def __load_fields(self):
>       private_fields = self.__scheme._meta.private_fields
E       AttributeError: 'Options' object has no attribute 'private_fields'

../../../.local/share/virtualenvs/tdd_test-D7r9ITf0/lib/python3.5/site-packages/mixer/backend/django.py:385: AttributeError
--------------------------------------------------- coverage: platform linux, python 3.5.3-final-0 ---------------------------------------------------
Coverage HTML written to dir htmlcov
================================= warnings summary ==================================================================
<undetermined location>
  pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0.  Please remove the prefix and use the @pytest.fixture decorator instead.

-- Docs: http://doc.pytest.org/en/latest/warnings.html
========================= 1 failed, 1 warnings in 2.75 seconds ========================================================

Tags: djangoinpytestselfsharefieldspytest