首先,我不知道您的代码中的“服务器”是什么,但是我希望它会异,因此在您的设置功能中拥有更多类似的东西:
function (callback) {
server.StartServer(function(){
callback();
});
}
其次,保留该节点 在每次测试之后和之前执行启动和拆卸功能 因此,我怀疑您正在启动服务器2次(因为在拆卸中,您并没有真正关闭它)。
题
试图通过node.js和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
解决方案
首先,我不知道您的代码中的“服务器”是什么,但是我希望它会异,因此在您的设置功能中拥有更多类似的东西:
function (callback) {
server.StartServer(function(){
callback();
});
}
其次,保留该节点 在每次测试之后和之前执行启动和拆卸功能 因此,我怀疑您正在启动服务器2次(因为在拆卸中,您并没有真正关闭它)。
其他提示
我花了最后几个小时来解决这个问题,而且已经很清楚的是,Nodeunit无法捕获和显示出异常的功能,这些功能后来由IO或SetteMeTimeout类型流程触发。考虑到JavaScript的运行方式,这不足为奇。一旦您确定没有例外,一切都可以正常工作,但是如果您的代码中有错误,您将收到“撤消测试”消息,而无需其他。这是我为解决我的问题所做的工作(以Restify路线为例):
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);
}
}
一旦我以这种方式理解了这个问题,就可以解决问题,因为更加清晰,我能够通过所有测试来通过!
我怀疑你实际上没有打电话 test.done()
在第二个测试中。放 console.log()
打电话到那里验证您实际上是在打电话。
fwiw,我在下面使用您的测试的简化版本来重复描述的问题。如果您省略了 on('error', function() {...})
处理程序,然后第二次测试未能完成。因此,我的理论是你 /push
端点正在触发恢复模块中的不同行为。即你 当然 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();
});
};
我正在围绕它进行工作,将服务器分配到设置中的过程中,然后在拆卸中杀死它。认为问题与创建的服务器有关,而不是关闭。感谢@matteofigus。
var cp = null; // child process
exports["tests"] = {
"setUp": function (callback) {
cp = fork("./lib/server.js", {"silent": true});
callback();
},
"tearDown": function (callback) {
cp.kill("SIGHUP");
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();
});
},
"TakeThisInfo": function (test) {
test.expect(1);
client.put("/push", {
"hello": "world"
}, function (err, req, res, data) {
test.ok(false);
test.done();
});
}
};