Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我怎样才能模拟一个多领域?_Python_Django_Testing_Mocking - Fatal编程技术网

Python 我怎样才能模拟一个多领域?

Python 我怎样才能模拟一个多领域?,python,django,testing,mocking,Python,Django,Testing,Mocking,我有这些模型要测试: # models.py (simplified, name is a costum multilanguage field) class NameType(models.Model): name = models.CharField(_('nome'), max_length=25, unique=True) class NameLanguage(models.Model): name = models.CharField(_('nome'), max_l

我有这些模型要测试:

# models.py (simplified, name is a costum multilanguage field)
class NameType(models.Model):
    name = models.CharField(_('nome'), max_length=25, unique=True)

class NameLanguage(models.Model):
    name = models.CharField(_('nome'), max_length=25, unique=True)
    syntax = models.ManyToManyField(
        NameType, related_name='syntax_name',
        verbose_name=_('sintassi'))
为了隔离要使用mock的测试,我已经测试了NameType

但是,当我尝试将self.name和self.姓氏添加到M2M语法时,会出现以下错误:

Traceback (most recent call last):
  File "E:\progetti\ElencoNomi\lists\tests\test_models.py", line 79, in test_language_created
    self.romans.syntax.add(self.name)
  File "e:\progetti\envs\ElencoNomi\lib\site-packages\django\db\models\fields\related_descriptors.py", line 938, in add
    through_defaults=through_defaults,
  File "e:\progetti\envs\ElencoNomi\lib\site-packages\django\db\models\fields\related_descriptors.py", line 1039, in _add_items
    if not router.allow_relation(obj, self.instance):
  File "e:\progetti\envs\ElencoNomi\lib\site-packages\django\db\utils.py", line
280, in allow_relation
    return obj1._state.db == obj2._state.db
  File "E:\Python\Python37\lib\unittest\mock.py", line 593, in __getattr__
    raise AttributeError("Mock object has no attribute %r" % name)
AttributeError: Mock object has no attribute '_state'
Traceback (most recent call last):
  File "E:\progetti\ElencoNomi\lists\tests\test_models.py", line 81, in test_language_created
    self.romans.syntax.add(self.name)
  File "e:\progetti\envs\ElencoNomi\lib\site-packages\django\db\models\fields\related_descriptors.py", line 938, in add
    through_defaults=through_defaults,
  File "e:\progetti\envs\ElencoNomi\lib\site-packages\django\db\models\fields\related_descriptors.py", line 1042, in _add_items
    (obj, self.instance._state.db, obj._state.db)
ValueError: Cannot add "<Mock spec='NameType' id='86024648'>": instance is on database "default", value is on database "<Mock name='mock._state.db' id='93279176'>"
我应该像上面代码中那样使用self.name和self.姓氏,还是只使用name和姓氏?有区别吗

多谢各位

编辑:就像我添加的评论中建议的那样

    self.name._state = Mock()
    self.surname._state = Mock()
但它给出了这样一个错误:

Traceback (most recent call last):
  File "E:\progetti\ElencoNomi\lists\tests\test_models.py", line 79, in test_language_created
    self.romans.syntax.add(self.name)
  File "e:\progetti\envs\ElencoNomi\lib\site-packages\django\db\models\fields\related_descriptors.py", line 938, in add
    through_defaults=through_defaults,
  File "e:\progetti\envs\ElencoNomi\lib\site-packages\django\db\models\fields\related_descriptors.py", line 1039, in _add_items
    if not router.allow_relation(obj, self.instance):
  File "e:\progetti\envs\ElencoNomi\lib\site-packages\django\db\utils.py", line
280, in allow_relation
    return obj1._state.db == obj2._state.db
  File "E:\Python\Python37\lib\unittest\mock.py", line 593, in __getattr__
    raise AttributeError("Mock object has no attribute %r" % name)
AttributeError: Mock object has no attribute '_state'
Traceback (most recent call last):
  File "E:\progetti\ElencoNomi\lists\tests\test_models.py", line 81, in test_language_created
    self.romans.syntax.add(self.name)
  File "e:\progetti\envs\ElencoNomi\lib\site-packages\django\db\models\fields\related_descriptors.py", line 938, in add
    through_defaults=through_defaults,
  File "e:\progetti\envs\ElencoNomi\lib\site-packages\django\db\models\fields\related_descriptors.py", line 1042, in _add_items
    (obj, self.instance._state.db, obj._state.db)
ValueError: Cannot add "<Mock spec='NameType' id='86024648'>": instance is on database "default", value is on database "<Mock name='mock._state.db' id='93279176'>"

但这真的是我想要的吗

Django使用模型和表来运行测试,有更复杂的库可以全面模拟您的数据,甚至在需要时在DB中创建数据,如

你可以这样做

import factory, factory.django
from . import models

class NameTypeFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = models.NameType 

class NameLanguageFactory(factory.django.DjangoModelFactory):
    @factory.post_generation
    def groups(self, create, extracted, **kwargs):
        if create and extracted:
            # A list of groups were passed in, use them
            for group in extracted:
                self.groups.add(group)

你试过这个吗?我使用model bakery进行模拟,但没有使用模拟的经验,因此无法给出答案,但听起来与您的问题一模一样。谢谢,但它会产生另一个错误。这不会产生错误,但它真的在测试我的代码吗?我这么问是因为我不太清楚到底发生了什么我正在研究是的,它是字段值的样板,使用所有代码,你可以使用像coverage这样的包来验证这一点