0
我試圖用BDD使用誓言開始我的node.js項目。 然後我得到了這個奇怪的錯誤。奇怪的錯誤,當我在node.js中使用誓言
我試着寫與誓言表達一個小的路線測試,這裏是我的原代碼,
node_server.prototype.mainpage = function(callback) {
http.get({host:'localhost', port:this.port, path:'/', agent:false}, function(res){
callback(res.statusCode);
});
}
這裏是我怎麼寫我的誓言測試
vows.describe('Request to the server').addBatch({
'Should get http 200': {
topic: function() {
app_server.mainpage(this.callback)
},
'we get 200': function (statusCode) {
app_server.close_server();
assert.equal(statusCode, 200);
}
},
}).run(); // Run it
誓言始終報告意外的錯誤,即使的StatusCode是正確的,這樣
» An unexpected error was caught: 200
所以我改變我的馬頁內廣告功能,這樣
node_server.prototype.mainpage = function(callback) {
http.get({host:'localhost', port:this.port, path:'/', agent:false}, function(res){
callback("error", res.statusCode); // Here I added a err message in front of the status code.
});
}
而且我也改變我的測試套件爲
'we get 200': function (err, statusCode) {
那麼這個測試工作! 我只是想知道這種奇怪的情況怎麼會發生。我讀過誓言的文件,但我沒有找到他們說的任何地方,我必須把2個參數,而不是1.
給我一些線索! 提前謝謝!