2016-11-20 4 views
0

私は、次のチャイのHttpテストを持っている:送信するときJavascriptオブジェクトをポストするときにChaiがTypeErrorを投げているのはなぜですか?

it("should simulate the API tier filling out a report", (done) => { 
    // Send some JSON 
    let aReport = {"name": "Jack"}; 

    chai.request(webapp.app) 
    .post('/report') 
    .set('content-type', 'application/json') 
    .send(aReport) 
    .end((err: any, res: any) => { 
     expect(err).to.be.null; 
     expect(res.body.report).to.be.ok; 
     expect(res).to.have.status(200); 
     done(); 
    }).catch((error) => { 
     console.error('ERROR:', error); 
     done(error); 
     }); 
    }); 

私は次のエラーを取得していますが、ログに記録:

TypeError: First argument must be a string or Buffer 
    at ClientRequest.OutgoingMessage.end (_http_outgoing.js:555:11) 
    at Test.Request.end (node_modules/superagent/lib/node/index.js:873:9) 
    at node_modules/superagent/lib/request-base.js:72:12 
    at Test.then (node_modules/superagent/lib/request-base.js:71:31) 
    at Test.exports.catch (node_modules/superagent/lib/request-base.js:81:15) 
    at Context.it (test/report.spec.ts:57:13) 

それは私の理解してセンドのメソッドシグネチャですPOJOを受け入れ、適切な投稿を処理します。

質問: 上記の方法を使用すると、正しくJSONオブジェクトをchai-httpを使用してポストするのですか?

答えて

0

スタックトレースを見ると、chai-httpではなく、エラーの原因となっているのは/reportハンドラ(おそらくres.end()を数値引数と呼んでいます)だと思います。

0

これは、request.end()が2xと呼ばれているようです。これは、end()がcatch()メソッドを提供している間、catch()メソッドはthen()の後にのみ呼び出すことができるためです。

が、私はこの使用して非同期のリファクタリング/以下に待つとtypescriptです:

it("should simulate the API tier filling out a report", async() => { 

let aReport = {"name": "Jack"}; 

var request = chai.request(webapp.app) 
.post('/report') 
.set('content-type', 'application/json'); 

try { 
    let res = await request.send(aReport); 
    expect(res.body).to.be.ok; 
    expect(res.body.id).to.be.ok; 
    expect(res).to.have.status(200); 
} catch (e) { 
    expect(e).to.be.null; 
} 
}); 
-1

catch節は私の問題を修正削除します。つまり、次の句を削除します。

.catch((error) => { 
    console.error('ERROR:', error); 
    done(error); 
    }); 
関連する問題