2014-01-21 104 views
1

我有一個簡單的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); 
    }); 
}; 
+0

我來到這裏是因爲出現在「應該」信息上的**(a +旋律)字符。我發現我沒有將控制檯的輸出(在我的情況下,Windows 10下的Git Bash)設置爲UTF-8,然後這個字符看起來像一個複選標記:**√** – Fuhrmanator

回答

2

當你做到這一點,添加return;,以避免調用done回調的兩倍。這是爲摩卡,但也爲一般node.js回調處理。

if (err) { 
     console.log("Unable to create cabinet"); 
     done(err); 
     return; 
    } 
在你的櫥櫃模式

同樣的問題:

if (err) cb(err,null); 

需要退貨或將調用回調兩次並造成混亂(也親切地稱爲之間Node.js的博客爲一體的味道「釋放Zalgo「)。

+0

修改其中一個模式方法就像你所建議的那樣,我能夠做到這一點:'cabinetSchema.statics.isAuthorizedBorrower = function(cabinet_id,borrowername,cb)this.findOne({_ id:cabinet_id},function(err,cabinet){if { ERR)CB(ERR,NULL);如果 (hasBorrower(櫃,borrowername!)){ CB(新的錯誤(errorMsgs.borrowerNotAuthorized),NULL); 回報; } CB(NULL,borrowername);} ); };' –

+0

我不能說,在這個時候我明白*爲什麼* - 你也根據你的意見我會徹底閱讀這篇文章:http://blog.izs.me/post/59142742143/designing- API的換異步 –

相關問題