socket.io-client-dart icon indicating copy to clipboard operation
socket.io-client-dart copied to clipboard

How to mock a socket connection in a test ?

Open ValentinVignal opened this issue 1 year ago • 1 comments

I have a production code that looks like that:

final socket = io('/my-url')..connect();

socket.on('myEvent', myCallback);
test('', () {
   // I want to trigger 'myEvent' so `myCallback` is executed.
});

Looking around in the code, I saw that when a Socket is created, it creates a Transports.newInstance, which in the case of the test, uses the io implementation in io_transports.dart and returns an IOWebSocketTransport. It then creates a WebSocket.connect from dart:http which uses in the end HttpClient from dart:http.

HttpClient can be mocked tests using HttpOverrides.runZoned.

Is it possible to provide an example of how to mock the HTTP client or anything else to write tests?

ValentinVignal avatar Jun 15 '23 08:06 ValentinVignal

any updates?

magamal avatar Apr 16 '24 20:04 magamal