Команда управления Django: my_custom_command.py
from django.core.management.base import BaseCommand
from services.external import ExternalApi
class Command(BaseCommand):
def handle(self, *args, **options):
api = ExternalApi()
api.my_custom_method("param")
Код теста:
from django.core.management import call_command
from myapp.management.commands import my_custom_command
def test_custom_command(mocker):
mocker.patch.object(my_custom_command, 'ExternalApi')
call_command('my_custom_command')
my_custom_command.ExternalApi.my_custom_method.assert_called_with('param')
Результат:
def test_custom_command(mocker):
mocker.patch.object(my_custom_command, 'ExternalApi')
call_command('my_custom_command')
> my_custom_command.ExternalApi.my_custom_method.assert_called_with('param')
E AssertionError: Expected call: my_custom_method('param')
E Not called
Несмотря на то, что my_custom_method был вызван, тест не смог найти вызов метода. Вроде бы контекст отсутствует. Не могли бы вы помочь?