Javascript Mocha测试未按正确顺序运行,请使用before/beforeach
我使用下面的代码,在我复制文件之前,测试之后使用断言,但是当我运行它时,before/each在测试之后发生,我缺少什么 我也尝试过beforeach,但没有成功Javascript Mocha测试未按正确顺序运行,请使用before/beforeach,javascript,node.js,unit-testing,mocha.js,Javascript,Node.js,Unit Testing,Mocha.js,我使用下面的代码,在我复制文件之前,测试之后使用断言,但是当我运行它时,before/each在测试之后发生,我缺少什么 我也尝试过beforeach,但没有成功 describe(" Handler", function() { before((done) => { fs.stat(utils.pathSetup() + "/ins/", function (err, stats) { if (!err) {
describe(" Handler", function() {
before((done) => {
fs.stat(utils.pathSetup() + "/ins/", function (err, stats) {
if (!err) {
console.log("success");
} else {
let lclpath = utils.pathSetup();
Loder.add({folderPath: lclpath + "/test/testcontent/ins"});
console.log(err);
}
});
done();
});
//这是在前面的语句之前调用的为什么?????
//这应该发生在before完成之后,它们在同一个测试块中,这个调用是在
Handler.invoke(req, res)
.then((Ref) => {
}).done();
ee.on('Started', (arg) => {
evtResStart = arg;
});
在调试中,当我放置BP时,它会停止在before中,并单击步骤将我带到Handler.invoke,而不是before…:(
你知道这样做的原因是什么吗?如果你在测试中使用
done
,你需要在回调中调用done()
,例如
before((done) => {
fs.stat(utils.pathSetup() + "/ins/", function (err, stats) {
if (!err) {
console.log("success");
} else {
let lclpath = utils.pathSetup();
Loder.add({folderPath: lclpath + "/test/testcontent/ins"});
console.log(err);
}
done();
});
});
编辑:假设问题中的注释被格式化为代码,那么您的处理程序是否在it
块中
it('should do something', () => {
Handler.invoke(req, res)
.then((Ref) => {
}).done();
ee.on('Started', (arg) => {
evtResStart = arg;
});
});
您是否尝试过将done();
放入的回调函数中?
通过这种方式,您的测试应该按照您想要的方式运行
before((done) => {
fs.stat(utils.pathSetup() + "/ins/", function (err, stats) {
if (!err) {
console.log("success");
} else {
let lclpath = utils.pathSetup();
Loder.add({folderPath: lclpath + "/test/testcontent/ins"});
console.log(err);
}
done();
});
});
谢谢:),我试过了,但它不起作用:(还有其他想法吗?请跳一下:fs.statSync()
也应该可以解决这个问题,因为实际上不需要异步运行它。