Skip to content

TestAsyncClient broken in 0.30.8 - AttributeError: 'Controller' object has no attribute 'set_api_instance' #335

@jdkizer9

Description

@jdkizer9

After upgrading from 0.30.7 to 0.30.8, TestAsyncClient fails when making requests with:

AttributeError: 'Controller' object has no attribute 'set_api_instance'
The error occurs when the first request is made (not during client instantiation) because the urls property is lazy-evaluated.

Minimal Reproduction

import pytest
from django.test import TestCase
from ninja_extra import api_controller, ControllerBase, route
from ninja_extra.testing import TestAsyncClient


@api_controller('/test')
class TestController(ControllerBase):
    @route.get('/hello')
    async def hello(self):
        return {"message": "hello"}


@pytest.mark.django_db
class TestBug(TestCase):
    def setUp(self):
        self.controller = TestController()
        self.client = TestAsyncClient(self.controller)

    @pytest.mark.asyncio
    async def test_request_fails(self):
        # This line triggers the bug in 0.30.8
        response = await self.client.get('/hello')
        self.assertEqual(response.status_code, 200)

Traceback

hometeam_app/tests/test_ninja_extra_async_client.py:61: in test_async_client_can_make_requests
    response = await self.client.get('/hello')
/usr/local/lib/python3.12/site-packages/ninja/testing/client.py:42: in get
    return self.request("GET", path, data, **request_params)
/usr/local/lib/python3.12/site-packages/ninja_extra/testing/client.py:63: in request
    func, request, kwargs = self._resolve(
/usr/local/lib/python3.12/site-packages/ninja_extra/testing/client.py:39: in _resolve
    for url in self.urls:
/usr/local/lib/python3.12/site-packages/ninja/testing/client.py:113: in urls
    self.router_or_app.set_api_instance(api)
AttributeError: 'TestController' object has no attribute 'set_api_instance'

Root Cause
The 0.30.8 release includes commit c70781d ("refactor: migrate controller metadata to reflect-based storage") which moved controller metadata from direct attributes to reflect-based storage. However, ninja/testing/client.py still calls self.router_or_app.set_api_instance(api) on controllers, which no longer have this method.
Environment
django-ninja-extra: 0.30.8 (broken)
django-ninja-extra: 0.30.7 (works)
django-ninja: 1.5.0
Python: 3.12
Django: 5.2
Workaround
Pin to django-ninja-extra<=0.30.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions