0

I am having trouble stubbing out a celery task.

Here is the task itself:

@celery.task
def hello_task:
  print("hello")

Here is my service calling the task:

class Hello:
    def __init__(self, context: ContextObject):
        self.context = context

    def call_task(self) -> None:
        hello_task.delay()

I am writing a test to test call_task in my Hello class:

    @patch('path.to.hello_task')
    def test_create_attestations(self, task_mock):
        self.service.call_task()
        task_mock.assert_called()

The problem that I have is that the @patch statement is not stubbing out the call to hello_task so it is still printing hello. I want to stub out the celery task completely and just assert that it has been called.

Any tip?

Huy
  • 10,806
  • 13
  • 55
  • 99
  • before we dive in to any solutions do you understand why this is happening? you are patching the _function_ but not the decorator – gold_cy Apr 01 '19 at 18:43
  • @aws_apprentice yes, I understand that, but I also tried @patch('celery.task') which didn't work so that's why I am posting here – Huy Apr 01 '19 at 18:52
  • you should check out --> https://stackoverflow.com/questions/7667567/can-i-patch-a-python-decorator-before-it-wraps-a-function – gold_cy Apr 01 '19 at 18:53
  • @aws_apprentice thx – Huy Apr 01 '19 at 19:00

0 Answers0