How do I mock a django signal handler?

后端 未结 7 1616
走了就别回头了
走了就别回头了 2020-12-14 00:22

I have a signal_handler connected through a decorator, something like this very simple one:

@receiver(post_save, sender=User, 
          dispatch_uid=\'myfil         


        
相关标签:
7条回答
  • 2020-12-14 00:52

    Possibly a better idea is to mock out the functionality inside the signal handler rather than the handler itself. Using the OP's code:

    @receiver(post_save, sender=User, dispatch_uid='myfile.signal_handler_post_save_user')
    def signal_handler_post_save_user(sender, *args, **kwargs):
      do_stuff()  # <-- mock this
    
    def do_stuff():
       ... do stuff in here
    

    Then mock do_stuff:

    with mock.patch('myapp.myfile.do_stuff') as mocked_handler:
        self.assert_equal(mocked_handler.call_count, 1)
    
    0 讨论(0)
提交回复
热议问题