0

私はnode.jsを初めて使用し、ポート5000で実行される小さなWebアプリを作成することから始めました。ローカルで(ブラウザーまたはcurlを介して)実行されるこのURLを試したところ、すべてが正常に機能し、応答。しかし、「誓約を使用してBDD」を実行しているときにhttpクライアントに接続しようとすると、テストが失敗し、結果のメッセージは次のようになりました。

✗ 

  /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);

これは私がアプリケーション内でどのように応答しているかです。ヘッダーのcontent-typeとcontent-lengthを設定しました。誰かが問題になるかもしれないことについて私を助けてくれますか?

私が書いた誓いはこれです。

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(); 
4

2 に答える 2

2

コードを変更し、応答をコールバックの最初のパラメーターとして、エラーを 2 番目のパラメーターとして試しました。これは私にとってはうまくいきました。

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

ただし、上記のように、唯一のパラメーターとしての res は依然としてエラーを発生させます。
サポートしてくれてありがとう。

于 2012-01-04T07:12:59.797 に答える
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

于 2012-01-03T17:39:14.827 に答える