简体   繁体   English

使用VM服务器和dartium浏览器进行Dart集成测试

[英]Dart integration test with VM server and dartium browser

I'm making a library that implements both server and client parts that interacts between them via websockets: 我正在制作一个实现服务器和客户端部分的库,它们通过websockets在它们之间进行交互:

Server use example (ran in CLI): 服务器使用示例(在CLI中运行):

Server srv = await new Server("localhost:1234");
srv.onNewClientConnected.listen(print("client connected"));

Client use example (ran in browser): 客户端使用示例(在浏览器中运行):

Client cli = await new Cliente("localhost:1234");
cli.sendCommand(...);

(Just by creating the instances, the client should be connected and the server noticed about that connection.) (仅通过创建实例,客户端应已连接,服务器已注意到该连接。)

I'd like to know what would be the best way to test their interactions? 我想知道什么是测试他们互动的最佳方法? Could I check both objects internals with that method? 我可以使用该方法检查两个对象的内部吗?

I would like something like this: 我想要这样的东西:

test(".echo should receive same input from server", (){
   cli.echo("message");
   expect(srv.lastMessageReceived, equals("echo: message"));
   expect(cli.lastResponseReceived, equals("echo: message"));
   expect(srv.amountMessagesReceived, equals(1));
});

If I understand correctly, I'm guessing you are trying to encapsulate https://www.dartlang.org/dart-vm/dart-by-example#websockets into helpers so that you have only instances when connected. 如果我理解正确,我猜您正在尝试将https://www.dartlang.org/dart-vm/dart-by-example#websockets封装到帮助器中,以便仅在连接时具有实例。 However both operations (server side binding/listening/upgrade, client side connection) is asynchronous so you will never reach the state you want by just creating the instances (or you will need an additional asynchronous methods to be notified). 但是,这两种操作(服务器端绑定/侦听/升级,客户端连接)都是异步的,因此仅通过创建实例就永远不会达到所需的状态(否则,您将需要其他的异步方法来进行通知)。 I would suggest creating asynchronous helpers. 我建议创建异步帮助器。

Assuming you accept only one client in your server 假设您在服务器中仅接受一个客户端

Server server = await Server.accept("localhost:1234");

Client side: 客户端:

Client client = await Client.connect("localhost:1234");

By doing so, you will have only server and client instances when connected 这样,连接后将只有服务器和客户端实例

I like the https://pub.dartlang.org/packages/web_socket_channel package which provide a good abstraction and allow me to test my web socket client logic that will run in the browser in a simple io test. 我喜欢https://pub.dartlang.org/packages/web_socket_channel软件包,该软件包提供了很好的抽象,并允许我测试将在简单的io测试中在浏览器中运行的Web套接字客户端逻辑。

As for testing recommendations, I personally start my web socket server in setUpAll and create my client in setUp and user a similar logic that you propose (don't forget the await though as you will need to wait for the echo response). 至于测试建议,我个人在setUpAll启动我的Web套接字服务器,并在setUp创建我的客户端,并使用您建议的类似逻辑(但是请不要忘记等待,因为您将需要等待回显响应)。 Again the web_socket_channel package has some good testing example that you can look at ( https://github.com/dart-lang/web_socket_channel/tree/master/test ) 同样, web_socket_channel包中有一些不错的测试示例,您可以查看( https://github.com/dart-lang/web_socket_channel/tree/master/test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM