在我们的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
,每个测试用例都继承它(在继承链的第一个位置):
然而,在每个测试用例中添加这个mixin是非常麻烦和重复的,理想情况下,我们希望将这个补丁功能构建到测试工厂中。在
有没有办法在工厂里做到这一点?(我查看了源代码(https://github.com/FactoryBoy/factory_boy/blob/2d735767b7f3e1f9adfc3f14c28eeef7acbf6e5a/factory/django.py#L256),似乎__enter__
方法正在设置signal.receivers = []
;也许可以对其进行修改,使其接受receiver
函数并将其从signal.receivers
列表中弹出)?在
目前没有回答
相关问题 更多 >
编程相关推荐