首页 > 解决方案 > Mixer:创建实例时出现属性错误

问题描述

对于 TDD,我使用:

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

但问题是我无法通过混音器创建任何实例。他们显示了一个属性错误:

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

我的 test_models.py 和 models.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 ========================================================

标签: pythondjangotddpytest

解决方案


  1. 卸载当前的混音器: pip uninstall mixer==6.0.1
  2. 然后安装最新的混音器:pip install mixer

推荐阅读