尝试使用 Mocha 测试 Node.js 服务器进程 [英] Trying to test a Node.js Server process using Mocha

查看:20
本文介绍了尝试使用 Mocha 测试 Node.js 服务器进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Node.js 的新手

Fairly new to Node.js

制作了一个运行服务器进程并提供文件的应用程序(不使用 express 或任何框架),现在我正在尝试对其进行单元测试.

Made an app that runs a server process and serve files (does not use express or any frameworks), Now I'm trying to unit test it.

我正在尝试为此使用 mocha 测试...我打算启动我的服务器进程,然后针对它运行请求以测试预期结果(统计代码、正文内容等)

I'm trying to use a mocha test for that... I intended to start my server process and then run requests against it to test the expected results (stats code, body content and the likes)

但是它不能正常工作,所有请求都无法连接到服务器......我很确定问题是因为节点正在运行一个进程循环,而服务器没有在后台"运行在发出请求时,查询正在运行,或者服务器可能尚未运行(已启动 ASYNC)?

However it's not working properly, all the request fail to connect to the server... I'm pretty sure that the issue is because node is juts running one process loop, the server is not running "in the background" while the queries run or possibly the server is not running yet (started ASYNC) while the request are being made ?

无论如何,我想知道测试这个的正确方法是什么,我假设我需要让服务器在后台运行(如分叉进程)和/或者我需要找到一种方法来等待服务器进程首先启动",但不确定如何.

Anyway I was wondering what was the proper way to test this, I assume that either I need to have the server run in the background (like a forked process) and/or maybe I need to find a way to wait for the server process to be "up" first but not sure how.

或至少建议测试此类服务器进程(使用 Mocha 或其他).

Or at least recommendations on testing such server process (with Mocha or other).

谢谢.

这是示例测试代码(自原始问题以来已更新)

Here is example test code (Updated since original question)

var server = new Server302('./fixture/');

var instance;

describe('Tests', function() {

before(function(done) {
     instance = http.createServer(function(request, response) {
        console.log(request.url);
        server.serve(request, response);
    }).listen(8000);
    instance.on("listening", function() {
        console.log("started");
        done();
    });
});

after(function(done){
  instance.close();
  console.log("stopped");
  done();
});

it("Should fetch test.html", function(done) {
    console.log("test1");
    http.get("http://localhost:8000/", function(res) {
        res.on('data', function(body) {
            console.log(body)
            expect(body).toEqual("test");
            done();
        });
    })
});

它似乎按顺序执行,但仍然因连接错误而失败,而在使用浏览器手动测试时它可以工作:

It seem to Execute in order but still fails with a connection error, whereas it works when testing manually with the browser:

started
test1
․․․stopped


  ✖ 1 of 1 tests failed:

  1) Tests Should fetch test.html:
  Error: connect ECONNREFUSED
  at errnoException (net.js:670:11)
  at Object.afterConnect [as oncomplete] (net.js:661:19)

推荐答案

在你的 before 中不要调用 done 直到你得到由服务器.

In your before don't call done until you get the "listening" event fired by the server.

before(function(done) {
    instance = http.createServer(function(request, response) {
        console.log(request.url);
        server.serve(request, response);
    }).listen(8000);
    instance.on("listening", function() {
        console.log("started");
        done();
    });
});

这应该确保您的测试连接在服务器准备好之前不会启动.

That should ensure your test connections don't start before the server is ready.

另请参阅 server.listen 的文档

这篇关于尝试使用 Mocha 测试 Node.js 服务器进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆