使用 Node.js TCP 服务器进行测试驱动开发?

发布于 2025-01-05 06:05:59 字数 338 浏览 3 评论 0原文

我正在使用 Node.JS 编写一个简单的 TCP 服务器,以便在开始我想要的更大项目之前了解一些特定的事情。我希望能够了解更多有关测试驱动开发、用户身份验证和加密的知识。为了好玩,我正在实现一个带有用户帐户、统计数据和随机游戏配对的多人井字棋游戏。

反正。

我的问题是,我已经为应用程序的某些部分设置了测试,但现在是时候编写 TCP 部分了。我将 Expresso 与 Should.js 结合使用,效果很好。我的问题是如何测试 TCP 服务器?我是否反复提出请求并确保我得到我想要的东西?或者我应该以某种方式编写服务器,使其可以在不需要首先发出请求的情况下进行测试? (通过抽象)

我很好奇。预先感谢您的见解!

I'm writing a simplistic TCP server using Node.JS in order to learn more about some specific things before I take on a larger project that I have in mind. I'm hoping to learn more about test-driven-development, user authentication and encryption along the way. For kicks, I'm implementing a multiplayer tic-tac-toe game with user accounts, statistics, and random game pairing.

Anyway.

My question is, I have tests set up for some parts of the application, but it's come time to write the TCP portion. I'm using Expresso in combination with Should.js, and it's working great. My question is how exactly do I test a TCP server? Do I make repeated requests and make sure I'm getting back the things I'm wanting? Or should I somehow write the server in ways that it can be tested without having to make requests in the first place? (through abstraction)

I'm seriously curious. Thanks in advance for the insight!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡莣 2025-01-12 06:05:59

Node.js 让这变得非常简单。您可以直接在测试方法中创建服务器,并创建一个客户端以在该测试方法中进行连接。结果会非常干净。

您的客户端/服务器通信将具有某种消息传递/通信契约。因此,对于 TDD 这件事,您将从每个消息/数据集等开始,并为其编写测试。我喜欢让 Server 封装一个 TCP 服务器,以便它可以执行自定义逻辑。

所以你可能会得到这样的结果:

Test1:

describe('MyServer', function(){
  it('should respond with an acknowledgment of receiving my move command', function(done){
      var server = new MyServer();
      server.listen(9000);

      var json = '{"player": "1", "tile": "3"}' //player 1 puts an 'X' in tile 3
      client = net.connect(9000, function(){
        client.write(json);
      });

      client.on('data', function(data){

        //** your tests here to validate YOUR CUSTOM server response **
        //example assuming your server sends JSON
        serverResponse = JSON.parse(data.toString());
        assert(serverResponse.tilesRemainingCount, 5); //completely custom

        server.close();
        done();
      });
    });
}

这有帮助吗?我就是这么做的,效果很好。如果有不清楚的地方请告诉我,我会尽力解决。

另外,我是一名 CoffeeScript 开发人员,所以我可能在 JS 语法上犯了错误。

Node.js makes this really easy. You can create your server right in your test method and create a client to connect in that test method. It'll turn out very clean.

Your client/server communication will have some sort of messaging/communication contract. So, to TDD this thing, you'll start with each message/dataset, etc and write tests for that. I like to have Server encapsulate a TCP server so that it can perform custom logic.

So you might have something like this:

Test1:

describe('MyServer', function(){
  it('should respond with an acknowledgment of receiving my move command', function(done){
      var server = new MyServer();
      server.listen(9000);

      var json = '{"player": "1", "tile": "3"}' //player 1 puts an 'X' in tile 3
      client = net.connect(9000, function(){
        client.write(json);
      });

      client.on('data', function(data){

        //** your tests here to validate YOUR CUSTOM server response **
        //example assuming your server sends JSON
        serverResponse = JSON.parse(data.toString());
        assert(serverResponse.tilesRemainingCount, 5); //completely custom

        server.close();
        done();
      });
    });
}

Does that help at all? That's how I've been doing it, and it works great. Let me know if something's not clear and I'll try to clear it up.

Also, I'm a CoffeeScript dev, so I may have made an error with JS syntax.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文