'How to write class based testview for Django Channels using unittest module

I am trying to write unittests for my web sockets. I haven't wrote any previously so I am struggling understanding how to write using module unittest python package.

This is my consumer:

class DepartmentInfoConsumer(JsonWebsocketConsumer):

 def connect(self):
    if not self.scope["user"]:
        raise DenyConnection()
    self.accept()
    self.group_name = "test"

    async_to_sync(self.channel_layer.group_add)(f"department{self.scope['company'].id}", self.channel_name)
    departmentobject = Department.objects.get(company=self.scope["company"])
    self.send_json({"used_id": departmentobject.my_id})

 def disconnect(self, close_code):
    async_to_sync(self.channel_layer.group_discard)(f"department{self.scope['company'].id}", self.channel_name)
    raise StopConsumer()

 def used_id_info(self, event):
    self.send_json({"used_id": event["used_id"]})

MY URL:

websocket_urlpatterns = [
    re_path("bill/", consumers. DepartmentInfoConsumer.as_asgi(), name="billing-ws"),
]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source