django / channels

Developer-friendly asynchrony for Django
https://channels.readthedocs.io
BSD 3-Clause "New" or "Revised" License
6.02k stars 793 forks source link

cannot support use django reverse() path() to testing routing #2038

Closed fan9704 closed 11 months ago

fan9704 commented 11 months ago

Issues are for concrete, actionable bugs and feature requests only - if you're just asking for debugging help or technical support we have to direct you elsewhere. If you just have questions or support requests please use:

We have to limit this because of limited volunteer time to respond to issues!

Please also try and include, if you can:

Here is my routing.py

import logging

from channels.testing import ChannelsLiveServerTestCase, WebsocketCommunicator

from api.consumers.notifyConsumer import NotifyConsumer

logger = logging.getLogger(__name__)

websocket_urlpatterns = [
    path('ws/notify/<str:username>/', NotifyConsumer.as_asgi(), name='notify-consumer'),
    path('ws/chat/<str:username>/', ChatConsumer.as_asgi(), name='chat-consumer'),
]

My Testing test_routing.py

from django.test import TestCase, Client
from django.urls import reverse

class WebSocketRoutingTestCase(TestCase):
    def test_notify_consumer_routing(self):
        client = Client()
        url = reverse('notify-consumer', kwargs={'username': 'testuser'})
        response = client.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, b'WebSocket Handshake')

    def test_chat_consumer_routing(self):
        client = Client()
        url = reverse('chat-consumer', kwargs={'username': 'testuser'})
        response = client.get(url)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, b'WebSocket Handshake')

Daphne

Traceback (most recent call last):
  File "C:\Users\cxz12\PycharmProjects\PetMonitoringSystemBackend\api\tests\unit\routing\test_routing.py", line 41, in test_notify_consumer_routing
    url = reverse('notify-consumer', kwargs={'username': 'testuser'})
  File "C:\Users\cxz12\.virtualenvs\PetMonitoringSystemBackend-NwbnpaKV\lib\site-packages\django\urls\base.py", line 88, in reverse
    return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
  File "C:\Users\cxz12\.virtualenvs\PetMonitoringSystemBackend-NwbnpaKV\lib\site-packages\django\urls\resolvers.py", line 828, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'notify-consumer' not found. 'notify-consumer' is not a valid view function or pattern name.
carltongibson commented 11 months ago

Yes, it's not supported. See #1912.