试图加快node.js和nodeunit的速度,但我发现nodeunit有一个问题,在其中一个测试中,它没有看到对test.done()的调用。
守则:
// Added for clarity.
var client = require("restify").createJsonClient({
"version": "*",
"url": "http://localhost:" + server.Port
});
exports["tests"] = {
"setUp": function (callback) {
server.StartServer();
callback();
},
"tearDown": function (callback) {
callback();
},
"CanIHaveSomeTeaPlease?": function (test) {
test.expect(4);
client.get("/tea", function (err, req, res, data) {
test.equal(err.statusCode, 418, "Expected ImATeapot Error.");
test.equal(err.message, "Want a biscuit?", "Expected to be asked if I want a buscuit.");
test.equal(err.restCode, "ImATeapotError");
test.equal(err.name, "ImATeapotError");
test.done();
});
},
// Note: I expect this test to fail as it is a copy of the above
// test on a different url that doesn't return the ImATeapot
// HTTP error. But it doesn't look like it's detecting it
// properly.
"TakeThisInfo": function (test) {
test.expect(4);
client.put("/push", {
"hello": "world"
}, function (err, req, res, data) {
test.equal(err.statusCode, 418, "Expected ImATeapot Error.");
test.equal(err.message, "Want a biscuit?", "Expected to be asked if I want a buscuit.");
test.equal(err.restCode, "ImATeapotError");
test.equal(err.name, "ImATeapotError");
test.done();
});
}
};输出:
FAILURES: Undone tests (or their setups/teardowns):
- tests - TakeThisInfo
To fix this, make sure all tests call test.done()我希望这是件愚蠢的事。
版本:-
Node: 0.10.21
NPM: 1.3.11
Nodeunit: 0.8.2
Grunt-CLI: 0.1.10
Grunt: 0.4.1发布于 2013-11-09 23:06:44
首先,我不知道代码中的"server“是什么,但是我希望它是异步的,所以在您的setUp函数中有类似的内容:
function (callback) {
server.StartServer(function(){
callback();
});
}第二,保持节点单元在每次测试之后和之前执行startUp和tearDown函数,因此我怀疑您正在启动服务器2次(就像在tearDown中没有真正关闭它一样)。
发布于 2014-03-06 08:10:50
在过去的几个小时里,我一直在处理这个问题,现在已经很清楚的是,nodeunit无法捕获和显示在以后由IO或setTimeout类型进程触发的函数中抛出的异常。考虑到JavaScript的运行方式,这并不奇怪。一旦您确信没有异常,一切都会正常工作,但是如果代码中有错误,您将得到“未完成的测试”消息,而没有其他任何消息。以下是我为解决问题所做的工作(以重新定位路径为例):
function myRoute(req, res, next) {
try {
// your code goes here...
}
catch (err) {
// write it to the console (for unit testing)
console.log(err);
// pass the error to the next function.
next(err);
}
}一旦我以这种方式理解了这个问题,就会修复它,因为它更加清晰,并且我能够通过我所有的测试!
发布于 2013-11-09 14:21:06
我怀疑你并没有在第二次测试中给test.done()打电话。在那里放一个console.log()电话,以验证你确实在打那个电话。
FWIW,下面我使用简化的测试版本再现了所描述的问题。如果省略了on('error', function() {...})处理程序,那么第二个测试就无法完成。因此,我的理论是,您的/push端点正在触发restify模块中的不同行为。也就是说,您确定restify正在用err属性调用您的回调,还是它正在做一些不同的事情?例如,像http.get那样发出一个事件,如下所示。
var http = require('http');
exports.test1 = function (test) {
test.expect(1);
http.get({hostname: "www.broofa.com", path: "/"}, function (res) {
test.equal(res.statusCode, 200, 'got 200');
test.done();
});
};
exports.test2 = function (test) {
test.expect(1);
http.get({hostname: "www.no-such-domain.com", path: "/"}, function (res) {
test.equal(res.statusCode, 200, 'got 200');
test.done();
}).on('error', function() {
// Comment line below out to repro the "Undone tests" error
test.done();
});
};https://stackoverflow.com/questions/19836121
复制相似问题