socket.io-client-dart
socket.io-client-dart copied to clipboard
How to mock a socket connection in a test ?
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?
any updates?