开发者

In test cases(unit-testing), Django pre_save signal can not be caught

开发者 https://www.devze.com 2023-03-11 02:33 出处:网络
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?

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 h开发者_开发百科andler 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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号