在测试模式下断开模型信号

时间:2018-04-23 19:43:48

标签: django django-testing django-signals django-tests

有没有办法在测试模式Django 1.11中解除模型信号?

或者也许是一种创建没有ORM的对象来防止post_save方法发出信号的方法?

设置测试代码

def setUp(self):
   #some code
   with patch(post_save):
       self.instance = Instance.objects.create(fields)

错误:AttributeError: 'ModelSignal' object has no attribute 'rsplit'

1 个答案:

答案 0 :(得分:1)

这些信号不应该被嘲笑,但如果你真的需要这样做,这应该有效:

from unittest.mock import patch

def test_method(self):
    with patch('django.db.models.signals.post_save.send'):
        MyObject.objects.create()