在 Django 中,我的捕获 pre_save 信号的代码运行良好。但是,在 tests.py 中的测试用例中,信号处理程序无法接收任何东西。这个问题有什么提示吗?
- 我的测试用例和信号处理程序似乎在不同的应用程序中。这是问题的原因吗?
最佳答案
It seems that my testcases and signal handler are in different apps. Is this the cause of the problem?
是的。每个应用程序的 tests.py
都是原子的。 导入
您的信号注册码或在测试中的某处手动连接它们以确保它们正在收听:
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.
关于python - 在测试用例(单元测试)中,无法捕获 Django pre_save 信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6281168/