我想测试一下我的消费者
scope['url_route']
HttpCommunicator
或
ApplicationCommunicator
,该参数未设置。如何设置此参数?测试文档非常有限,没有任何关于此的文档
https://channels.readthedocs.io/en/latest/topics/testing.html
.
测试.py
from channels.testing import HttpCommunicator
import pytest
from my_app import consumers
@pytest.mark.asyncio
async def test_consumers():
communicator = HttpCommunicator(consumers.MyConsumer, 'GET', '/project/1')
response = await communicator.get_response()
assert response['status'] == 400
我的应用程序.py
import json
from channels.db import database_sync_to_async
from channels.generic.http import AsyncHttpConsumer
from projects import model
class MyConsumer(AsyncHttpConsumer):
async def handle(self, body):
_id = int(self.scope['url_route']['kwargs'].get('project_id', 1))
record = await database_sync_to_async(models.Project.objects.get)(id=_id)
data = json.dumps({"id": _id})
await self.send_response(200, data.encode('utf8'))