试图测试采用摩卡一个Node.js的服务器进程 [英] Trying to test a Node.js Server process using Mocha

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

问题描述

相当新的Node.js

提出,运行服务器进程和文件服务(不使用前preSS或任何框架)的应用程序,现在我想要单元测试它。

我试图使用一个测试摩卡......我打算开始我的服务器进程,然后运行要求反对它来测试预期结果(统计code,主体内容和喜欢)

但它不能正常工作,所有的请求无法连接到服务器...我pretty确保该问题是因为节点是运行一个过程回路突出部,服务器没有运行,在背景,而查询运行或可能是服务器尚未运行(开始异步),而要求正在作出?

无论如何,我想知道什么是检验正确的方法,我认为无论我需要在后台服务器上运行(如派生进程)和/或也许我需要找到一种方法来等待服务器进程是涨第一,但不知道如何。

或于测试这样的服务器进程(与摩卡或其他)。至少推荐

感谢。

下面是示例测试code(因为原来的问题已更新)

  VAR服务器=新Server302('./夹具/');VAR实例;描述(测试,函数(){前(函数(完成){
     例如= http.createServer(函数(请求,响应){
        的console.log(request.url);
        server.serve(请求响应);
    })听(8000);
    instance.on(听,函数(){
        的console.log(开始);
        完成();
    });
});后(函数(完成){
  instance.close();
  的console.log(停止);
  完成();
});它(应该获取的test.html功能(完成){
    的console.log(测试1);
    http.get(HTTP://本地主机:8000 /功能(RES){
        res.on(数据,功能(体){
            的console.log(体)
            期待(体).toEqual(测试);
            完成();
        });
    })
});

这似乎才能执行,但仍失败,出现连接错误,而它与浏览器中手动测试时的工作:

 启动
TEST1
...停止
  1 1✖测试失败:  1)测试应取的test.html:
  错误:连接ECONNREFUSED
  在errnoException(net.js:670:11)
  在Object.afterConnect [如的onComplete(net.js:661:19)


解决方案

在你的之前不叫完成直到你得到服务器打响了监听事件。

 之前(功能(完成){
    例如= http.createServer(函数(请求,响应){
        的console.log(request.url);
        server.serve(请求响应);
    })听(8000);
    instance.on(听,函数(){
        的console.log(开始);
        完成();
    });
});

这应确保之前的服务器准备好你的测试连接不启动。

又见<一个href=\"http://nodejs.org/api/net.html#net_server_listen_port_host_backlog_listeninglistener\">documentation对于server.listen

Fairly new to Node.js

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.

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)

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.

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

Thanks.

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)

解决方案

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.

See also the documentation for server.listen

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

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