Mocha测试在失败时执行两次回调 [英] Mocha test executes callback twice on failure

查看:94
本文介绍了Mocha测试在失败时执行两次回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在用Mocha测试的简单Mongoose模式;当我使用'success'回调运行测试时,它会正常执行,但是当最后一个测试执行时,它会失败并似乎再次运行该测试(我得到两个结论,一个结论填充了错误对象,第二个结论在错误对象中返回null.错误对象.)运行以下两个测试将得到以下输出:

I have a simple Mongoose schema that I'm testing with Mocha; when I run the test with 'success' callback it executes normally, however when the last test executes it fails and appears to run the test again (I get two conclusions, one which populates the error object and the second one which returns null in the error object.) Running the two tests below results in the following output:

Cabinet: 
  â should return all authorized  
  â should return not authorized  <-- it succeeds the first time?
  1) should return not authorized 


2 passing (42ms)  
1 failing         <--- what?  there are only two tests

1) Cabinet:  should return not authorized :
   Uncaught AssertionError: expected null to exist  <--- see test

此测试重复

it("should return not authorized error ", function(done){
    var newCabinet = {
        name:  "A new cabinet",
        description: "Some remote cabinet",
        authorizedBorrowers : ["imatest","imanothertest"],
        cabinetInventory : []
    };
    Cabinet.newCabinet(newCabinet, function(err, cabinet){
        if (err) {
            console.log("Unable to create cabinet");
            done(err);
        }
        Cabinet.isAuthorizedBorrower(cabinet._id, "bob", function(cberr, borrower){
            should.exist(cberr);  <-- 'expected null to exist' points here
            done();
        });
    });
});

此测试工作

it("should not return unauthorized error ", function(done){
    var newCabinet = {
        name:  "A new cabinet",
        description: "Some remote cabinet",
        authorizedBorrowers : ["imatest","imanothertest"],
        cabinetInventory : []
    };
    Cabinet.newCabinet(newCabinet, function(err, cabinet){
        if (err) {
            console.log("Unable to create cabinet");
            done(err);
        }
        //console.log("ID " + cabinet._id)
        Cabinet.isAuthorizedBorrower(cabinet._id, "imatest", function(cberr, borrower){
            should.not.exist(cberr);
            done();
        });
    });
});

架构

var cabinetSchema = new Schema({
  name:  String,
  description: String,
  thumbnail : Buffer,
  authorizedBorrowers : [],
  defaultCheckout : {type : Number, default: 0} // default checkout mins = no time

});

var hasBorrower = function(cabinet, borrower){
  if (cabinet===null) return false;
  if (cabinet.authorizedBorrowers.length === 0) return false;
  return (cabinet.authorizedBorrowers.indexOf(borrower) >= 0)
}  

cabinetSchema.statics.isAuthorizedBorrower = function(cabinet_id, borrowername, cb ){
  this.findOne({_id: cabinet_id}, function(err, cabinet){
    if (err) cb(err,null);
    if (!hasBorrower(cabinet, borrowername)) cb(new Error(errorMsgs.borrowerNotAuthorized),null);
    cb(null,borrowername);
  });
};

推荐答案

无论何时执行此操作,都添加一个return;以避免两次调用done回调.这既适用于摩卡咖啡,也适用于常规的node.js回调处理.

Whenever you do this, add a return; to avoid calling the done callback twice. This is for mocha but also for general node.js callback handling.

    if (err) {
        console.log("Unable to create cabinet");
        done(err);
        return;
    }

内阁架构中的相同问题:

Same problem in your Cabinet schema:

if (err) cb(err,null);

需要返回,否则它将两次调用回调并引起混乱(在node.js博客圈中也被亲切地称为释放Zalgo"的一种味道).

That needs a return or it will invoke the callback twice and cause chaos (also affectionately known amongst the node.js blogosphere as one flavor of "releasing Zalgo").

这篇关于Mocha测试在失败时执行两次回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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