我怎样才能模仿一个芹菜助教的上课方法

2024-10-02 00:44:35 发布

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

使用Python2.7、celery 3.0.24和mock 1.0.1。我有这个:

class FancyTask(celery.Task):
    @classmethod
    def helper_method1(cls, name):
        """do some remote request depending on name"""
        return 'foo' + name + 'bar'

    def __call__(self, *args, **kwargs):
        funcname = self.name.split()[-1]
        bigname = self.helper_method1(funcname)
        return bigname


@celery.task(base=FancyTask)
def task1(*args, **kwargs):
    pass

@celery.task(base=FancyTask)
def task2(*args, **kwargs):
    pass

在测试这两个任务时,如何修补helper_method1?在

我试过类似的方法:

^{pr2}$

测试失败了。原始函数就是被调用的函数。不,this question帮不了我。在


Tags: nameselfhelpertaskbasereturndefargs
1条回答
网友
1楼 · 发布于 2024-10-02 00:44:35

(我没有启动并运行celery实例,因此很难对其进行测试)

应用程序代码中的目标函数是类方法。测试代码模拟的函数是一个实例方法。在

像这样更改测试任务1是否有助于-

^{1}$

您可能还需要更改assert_called_with,以便从类级别而不是实例级别调用它。在

改变

^{pr2}$

     FancyTask.helper_method1.assert_called_with('blah')

相关问题 更多 >

    热门问题