How can i autospec a mocked Celery function

celery, mocking, python, unit-testing

Solution

You are actually trying to test the arguments to the Task.run() function.

See the docs below: http://celery.readthedocs.org/en/latest/userguide/tasks.html#custom-task-classes http://celery.readthedocs.org/en/latest/reference/celery.app.task.html#celery.app.task.Task.run

run() is called with the arguments supplied to the task. If you want to make sure that the correct arguments are being supplied during your testing, then do as follows:

from mock import create_autospec
from celery import task

@task
def function(a, b, c):
    pass

mock_function = create_autospec(function)
mock_function.run('wrong arguments')

Then you should see the error you expect in the output

TypeError: <lambda>() takes exactly 4 arguments (2 given)

Problem

Given a celery task being called what is the simplest way to mock the function with autospec? For example in python Python 2.7.5 this code will pass fine: ``` from mock import create_autospec from celery import task @task def function(a, b, c): pass mock_function = create_autospec(function) mock_function.delay('wrong arguments') ``` When it should raise an exception because the celery 'delay' method will accept any parameter.

Original source