2014-01-13 4 views
27

私はsupertestで明示的なAPIをテストしています。httpコールをsuperagent/supertestでチェーンする方法は?

supertestを使用するためのテストケースで複数のリクエストを取得できませんでした。以下は、私がテストケースで試したものです。しかし、テストケースでは、HTTP GETである最後の呼び出ししか実行されていないようです。

it('should respond to GET with added items', function(done) { 
    var agent = request(app); 
    agent.post('/player').type('json').send({name:"Messi"}); 
    agent.post('/player').type('json').send({name:"Maradona"}); 
    agent.get('/player').set("Accept", "application/json") 
     .expect(200) 
     .end(function(err, res) { 
      res.body.should.have.property('items').with.lengthOf(2); 
      done(); 
    }); 
); 

ここでは何も不足していますか、またはHTTPコールをスーパーエージェントにチェーンする別の方法がありますか?

答えて

18

呼び出しは非同期に行われるため、コールバック関数を使用して呼び出しを連鎖させる必要があります。

it('should respond to GET with added items', function(done) { 
    var agent = request(app); 
    agent.post('/player').type('json').send({name:"Messi"}).end(function(){ 
     agent.post('/player').type('json').send({name:"Maradona"}).end(function(){ 
      agent.get('/player') 
       .set("Accept", "application/json") 
       .expect(200) 
       .end(function(err, res) { 
        res.body.should.have.property('items').with.lengthOf(2); 
        done(); 
       }); 
     }); 
    }); 
}); 
+9

コールバックのネストを減らすために[supertest-as-promised](https://www.npmjs.org/package/supertest-as-promised)をご覧ください –

+0

[co-supertest]を見てくださいhttps://github.com/avbel/co-supertest) – djv

+0

'mocha'や' it'を使わずに連鎖させることはできますか? – gurvinder372

51

これをコメントに入れようとしましたが、フォーマットがうまくいかなかった。

私は非同期を使用しています。これはです。実際には標準であり、本当にうまく動作します。

it('should respond to only certain methods', function(done) { 
    async.series([ 
     function(cb) { request(app).get('/').expect(404, cb); }, 
     function(cb) { request(app).get('/new').expect(200, cb); }, 
     function(cb) { request(app).post('/').send({prop1: 'new'}).expect(404, cb); }, 
     function(cb) { request(app).get('/0').expect(200, cb); }, 
     function(cb) { request(app).get('/0/edit').expect(404, cb); }, 
     function(cb) { request(app).put('/0').send({prop1: 'new value'}).expect(404, cb); }, 
     function(cb) { request(app).delete('/0').expect(404, cb); }, 
    ], done); 
}); 
+1

これは受け入れられる回答です。非同期は、この文脈では幻想的に働く。 – csvan

+1

ここでasyncを見つけることができます:https://github.com/caolan/async –

+0

素晴らしいアプローチ!私は多少それを拡張し、ここで確認してくださいhttps://stackoverflow.com/a/45198533/449227 –

9

これが最もエレガントな約束を解決することができ、かつsupertestとの約束を使用するために本当に便利なライブラリがあります:https://www.npmjs.com/package/supertest-as-promised

彼らの例:

return request(app) 
    .get("/user") 
    .expect(200) 
    .then(function (res) { 
    return request(app) 
     .post("/kittens") 
     .send({ userId: res}) 
     .expect(201); 
    }) 
    .then(function (res) { 
    // ... 
    }); 
+0

約束はここに良いオプションです。そのチェーンのどこかで404から回復する必要がある状況に疲れているだけです。 – backdesk

+0

supertestはバージョン[2.0.0](https://github.com/visionmedia/supertest/blob/master/History.md#200--2016-07-29)からネイティブな約束をサポートしています – ricca

2

私はTim’s reply上に構築されたが、async.waterfallを使用代わりに、結果のアサートテストを行うことができるように(注:ここではMochaの代わりにTapeを使用します):

test('Test the entire API', function (assert) { 
    const app = require('../app/app'); 
    async.waterfall([ 
      (cb) => request(app).get('/api/accounts').expect(200, cb), 
      (results, cb) => { assert.ok(results.body.length, 'Returned accounts list'); cb(null, results); }, 
      (results, cb) => { assert.ok(results.body[0].reference, 'account #0 has reference'); cb(null, results); }, 
      (results, cb) => request(app).get('/api/plans').expect(200, cb), 
      (results, cb) => request(app).get('/api/services').expect(200, cb), 
      (results, cb) => request(app).get('/api/users').expect(200, cb), 
     ], 
     (err, results) => { 
      app.closeDatabase(); 
      assert.end(); 
     } 
    ); 
}); 
+1

あなたは私をたくさん救った時間。現在、それは最高の答えです。 –

関連する問題