Testing coroutine function in async web socket consumers using channels

Viewed 281

I am writing test cases for an Async WebSocket Consumer using the django channels. I want to mock a coroutine which I have defined in the connect method of my WebSocketConsumer.

Consumer.py

class ExampleTestConsumer(AsyncWebSocketConsumer):                                                                                          
                   async def connect(self):
                       await function_to_be_tested(args)
                       await self.accept()

Test case

class TestExampleTestConsumer:
           async def test can_connect_to_server(self,settings):
                     communicator = WebsocketCommunicator(
            application=application,
            path="/ws/example-test/1",
        )
             connected, _ = await communicator.connect()
             assert connected is True
             await communicator.disconnect()

I want the mocking of function_to_be_tested in this test case for this test case to work . Any suggestions ?

0 Answers
Related