How do I mock the bound context, or mock the celery task id?
Given a celery task like:
helpers.py:
from task import some_task
def some_helper():
some_task.delay(123)
in task.py:
@app.task(queue="abc", bind=True)
def some_task(self, some_number: int):
print(self.id) # how to mock this attribute access?
Simple test case:
from django.test.testcases import TestCase
from helpers import some_helper
class SomeTest(TestCase):
def test_some_helper(self):
some_helper()
I tried:
@patch("celery.app.base.Celery.task", return_value=lambda x: x)
I also tried:
class MockResult(dict):
def __getattr__(self, x):
return self[x]
...
def test_some_task(self):
cls = MockResult({"id": "asdf"})
bound_some_task = some_task.__get__(cls, MockResult)
bound_some_task(123)
Related: