2012-01-03 104 views
0

我是node.js的新手,並開始創建一個運行在端口5000的小型web應用程序。當我嘗試使用在本地運行的此url(通過瀏覽器或通過捲曲),一切正常,我得到迴應。但是當我試圖同時做「BDD用誓言」將其與HTTP客戶端連接,測試破了,結果消息是當使用http客戶端連接時,Node.js響應未定義

✗ 

    /GET 
    ✗ should respond with 404 
    ***TypeError: Cannot read property 'status' of undefined*** 
    at ClientRequest.<anonymous> (/home/sunil/work/nodal_programs/subscription-engine-processor/sample-test.js:13:23) 
    at runTest (/home/sunil/work/nodal_programs/subscription-engine-processor/node_modules/vows/lib/vows.js:132:26) 
    at EventEmitter.<anonymous> (/home/sunil/work/nodal_programs/subscription-engine-processor/node_modules/vows/lib/vows.js:85:17) 
    at EventEmitter.<anonymous> (events.js:67:17) 
    at EventEmitter.emit (/home/sunil/work/nodal_programs/subscription-engine-processor/node_modules/vows/lib/vows.js:236:24) 
    at /home/sunil/work/nodal_programs/subscription-engine-processor/node_modules/vows/lib/vows/context.js:31:52 
    at ClientRequest.<anonymous> (/home/sunil/work/nodal_programs/subscription-engine-processor/node_modules/vows/lib/vows/context.js:46:29) 
    at ClientRequest.<anonymous> (events.js:67:17) 
    at ClientRequest.emit (/home/sunil/work/nodal_programs/subscription-engine-processor/node_modules/vows/lib/vows.js:236:24) 
    at HTTPParser.onIncoming (http.js:1225:11) 
✗ Errored » 1 errored (0.012s) 

這裏的反應是不確定的。

body = "Not found"; 
response.writeHead(404, {'Content-Type': 'text/plain', 'Content-Length': body.length});  
response.end(body); 

這就是我在應用程序內的反應。我已經設置了標題內容類型和內容長度。 任何人都可以幫我解決什麼可能是問題?

我寫的誓言就是這個。

var http = require('http'), 
    vows = require('vows'), 
    assert = require('assert'); 

vows.describe("notification").addBatch({ 
    "/GET": { 
    topic: function() { 
    http.get({host: 'localhost', port: 1337, path: '/', method: 'GET'}, this.callback) ; 
    }, 
    'should respond with 404': function(e,res) { 
     assert.equal(res.status, 404); 
    } 
    } 
}).run(); 
+2

你能發佈你的測試代碼嗎? – nicolaskruchten 2012-01-03 12:11:31

回答

1

嘗試改變

'should respond with 404': function(e,res) { 
    assert.equal(res.status, 404); 
} 

'should respond with 404': function(res) { 
    assert.equal(res.status, 404); 
} 

根據該Node.js HTTP docs,所述http.request(因此http.get)回調不走err參數。

2

我在代碼中做了一個改變,並嘗試將響應作爲第一個回調和錯誤的第一個參數。這對我有效。

'should respond with 404': function(res,e) { 
    assert.equal(res.status, 404); 
} 

然而,res是唯一的參數仍然會引起上面提到的錯誤。
感謝您的支持。

相關問題