在Django Factory Boy中,是否可以使特定接收器静音(不是所有信号)?

2024-06-08 01:59:37 发布

您现在位置:Python中文网/ 问答频道 /正文

在我们的Django项目中,我们有一个接收函数,用于User模型发送的post_save信号:

@receiver(post_save, sender=User)
def update_intercom_attributes(sender, instance, **kwargs):
    # if the user is not yet in the app, do not attempt to setup/update their Intercom profile.
    if instance.using_app:
        intercom.update_intercom_attributes(instance)

这个接收器调用一个外部API,我们希望在用factory_boy生成测试夹具时禁用它。然而,就我所能分辨的https://factoryboy.readthedocs.io/en/latest/orms.html#disabling-signals而言,人们所能做的就是使所有信号静音,而不是特定的接收器。在

目前,我们要做的是定义一个IntercomMixin,每个测试用例都继承它(在继承链的第一个位置):

^{pr2}$

然而,在每个测试用例中添加这个mixin是非常麻烦和重复的,理想情况下,我们希望将这个补丁功能构建到测试工厂中。在

有没有办法在工厂里做到这一点?(我查看了源代码(https://github.com/FactoryBoy/factory_boy/blob/2d735767b7f3e1f9adfc3f14c28eeef7acbf6e5a/factory/django.py#L256),似乎__enter__方法正在设置signal.receivers = [];也许可以对其进行修改,使其接受receiver函数并将其从signal.receivers列表中弹出)?在


Tags: theinstance函数if信号factorysavenot

热门问题