尝试使用MochatestingNode.js服务器进程

Node.js相当新颖

做了一个运行服务器进程并提供文件的应用程序(不使用快速或任何框架),现在我试图unit testing它。

我试图使用摩卡testing…我打算启动我的服务器进程,然后运行请求来testing预期的结果(统计代码,正文内容等)

然而,它不能正常工作,所有的请求无法连接到服务器…我敢肯定,这个问题是因为节点是运行一个进程循环,服务器没有运行“在后台”,而查询运行或者可能服务器还没有运行(开始ASYNC),而请求正在进行?

无论如何,我想知道什么是testing这个正确的方法,我假设要么我需要在后台运行服务器(如分叉进程)和/或者我需要find一种方法来等待服务器进程先“起来”,但不知道如何。

或者至lessbuild议testing这样的服务器进程(与摩卡或其他)。

谢谢。

这里是示例testing代码(从原始问题更新)

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(); }); }) }); 

它似乎执行顺序,但仍然失败,连接错误,而在浏览器手动testing时,它的工作原理:

 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直到你得到服务器发起的“监听”事件。

 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(); }); }); 

这应该确保您的testing连接在服务器准备就绪之前不会启动。

另请参阅server.listen的文档

也不得不面对大块进来的身体,这是最后的工作,以帮助别人:

 var Server302 = require('../lib/server302.js'), http = require('http'), assert = require("assert"); var server = new Server302('./fixture/'); var instance; describe('Tests', function() { before(function(done) { instance = http.createServer(function(request, response) { server.serve(request, response); }).listen(8100); instance.on("listening", function() { done(); }); }); after(function(done) { instance.close(); done(); }); it("Should fetch test.html", function(done) { console.log("test1"); var body = ""; http.get({host: "localhost", port:8100, path: "/"}, function(res) { res.on('data', function(chunk) { // Note: it might be chunked, so need to read the whole thing. body += chunk; }); res.on('end', function() { assert.ok(body.toString().indexOf("<a href='/dummy.txt'>") !== -1); assert.equal(res.statusCode, 200); done(); }); }) }); it("Should fetch dummy.txt", function(done) { http.get({host: "localhost", port:8100, path: "/dummy.txt"}, function(res) { res.on('data', function(body) { assert.equal(res.statusCode, 200); assert.ok(body.toString().indexOf("test") === 0); done(); }); }); }); it("Should get 404", function(done) { http.get({host: "localhost", port:8100, path: "/qwerty"}, function(res) { assert.equal(res.statusCode, 404); done(); }); }); }); 

使用SuperTest

这是一个使用SuperTest和Mocha的完整而直接的例子

 var server = new Server302('./fixture/'); var request = require('supertest'); describe('Tests', function() { it('Should fetch test.html', function(done) { request(server) .get('/') .expect('test', done); }); }); 

SuperTest允许您:

  • 使用SuperAgent请求您的服务器(比低级别的http代理更容易使用)。
  • 绑定你的服务器到一个短暂的端口,所以不需要跟踪端口(如果需要,你仍然可以手动完成)。
  • 使用适用于Mocha (或任何其他testing框架)的sugary expect方法 。