2

由于某些原因,视图中存在前信号和后信号的防止。

def view(*args):
    # disable pre- and post-save signals for MyModel
    my_model_object.save()
    # anable pre- and post-save signals for MyModel

如何模拟 django pre_save 和 post_save 信号以检查它们是否未被 MyModel 调用?(没有任何接收器可以检查它是否没有被调用)

我试过这个,但它不起作用:

def MyTestCase(TestCase):
    def setUp(self):
      super(MyTestCase, self).setUp()
      self.patcher = patch('django.db.models.signals.pre_save')
      self.mocked_pre_save = self.patcher.start()

    def test_pre_save_signal_not_sent(self):
      self.admin_post(url, post_data)
      pre_save_senders_list = [arg[1]['sender'] for arg in mocked_pre_save.send.call_args_list]
      self.assertNotIn(MyModel, pre_save_senders_list)
4

0 回答 0