我有一個簡單的Mongoose模式,我正在用Mocha測試;當我使用'success'回調運行測試時,它會正常執行,但是當最後一次測試執行失敗並且似乎再次運行測試時(我得到兩個結論,一個填充錯誤對象,另一個填充錯誤對象,而第二個則返回null錯誤對象)運行下面的結果兩種測試在下面的輸出:摩卡測試執行回調失敗兩次
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);
});
};
我來到這裏是因爲出現在「應該」信息上的**(a +旋律)字符。我發現我沒有將控制檯的輸出(在我的情況下,Windows 10下的Git Bash)設置爲UTF-8,然後這個字符看起來像一個複選標記:**√** – Fuhrmanator