我实现了signalR websocket类,现在我想进行一些单元测试: 这就是我所拥有的:
class WebSocket {
HubConnection _hubConnection;
String _environmentUrl;
HubConnectionState get connectionStatus => _hubConnection.state; // getter to recieve status
WebSocket(this._environmentUrl); // constructor
void connect() async {
_buildConnection();
try {
await _hubConnection.start();
print('Conected!');
} catch (exception) {
print('$exception something went weong!');
}
}
void _buildConnection() {
_hubConnection = HubConnectionBuilder().withUrl(_environmentUrl).build();
}
}
和我的单元测试:
void main() {
test('Should create a Websocket', () async {
WebSocket webSocket = WebSocket(EnvironmentHml.socketUrl);
webSocket.connect();
expect(webSocket.connectionStatus, HubConnectionState.Connected);
});
}
一切正常,但是当我运行单元测试时,我得到了:
Expected: HubConnectionState:<HubConnectionState.Connected>
Actual: HubConnectionState:<HubConnectionState.Disconnected>
因为连接方法是异步的,所以请不要理我