在测试用例(单元测试)中,Django pre_save信号不能被捕获 [英] In test cases(unit-testing), Django pre_save signal can not be caught

查看:151
本文介绍了在测试用例(单元测试)中,Django pre_save信号不能被捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django中,我的代码捕捉pre_save信号效果很好。但是,在tests.py中的测试用例中,信号处理程序无法接收任何内容。有没有提示这个问题?




  • 似乎我的测试用例和信号处理程序在不同的应用程序。这是否是问题的原因?


解决方案


我的测试用例和信号处理程序在不同的应用程序。这是否是问题的原因?


是的。每个应用程序的 tests.py 是原子的。 导入您的信号注册码或手动连接到您测试的某个地方,以确保他们正在收听:


您可以将信号处理和
注册码放在任何你喜欢的地方。
但是,您需要确保
的早期
导入的模块,以便在任何信号需要
之前,信号处理获得
注册发送。


(From: 收听信号,连接接收机功能


In Django, my code on catching pre_save signal works well. However, in testcases in tests.py, the signal handler cannot receive anything. Is there any hint for this problem?

  • It seems that my testcases and signal handler are in different apps. Is this the cause of the problem?

解决方案

It seems that my testcases and signal handler are in different apps. Is this the cause of the problem?

Yes. Each app's tests.py is atomic. import your signal registration code or connect them manually somewhere in your test to make sure they are listening:

You can put signal handling and registration code anywhere you like. However, you'll need to make sure that the module it's in gets imported early on so that the signal handling gets registered before any signals need to be sent.

(From: Listening to signals, Connecting receiver functions.)

这篇关于在测试用例(单元测试)中,Django pre_save信号不能被捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆