2016-06-20 23 views
0

Ich versuche, eine authentifizierte Route zu testen.So senden Sie eine Authentifizierung mit einer Anfrage mit Supertest

Dies ist mein Code:

let request = require('supertest'); 
var superagent = require('superagent'); 
var agent = superagent.agent(); 
var theAccount = { 
    name: '*********', 
    role: 'admin', 
    id: '115039452833383267752' 
}; 
request = request('http://localhost:3000'); 

describe('Live-score',() => { 

    before(function (done) { 
    request 
     .post('/api/login') 
     .send(theAccount) 
     .end(function (err, res) { 
     if (err) { 
      throw err; 
     } 
     agent.saveCookies(res); 
     done(); 
     }); 
    }); 

    it('Should work', (done) => { 
    agent.attachCookies(req); 
    request 
     .get('/api/live-score') 
     .send(agent) 
     .set('Accept', 'text/html') 
     .expect('Content-Type', 'application/json; charset=utf-8') 
     .expect(200, done); 

    }); 

Jedoch habe ich die folgende Fehlermeldung erhalten:

TypeError: agent.saveCookies is not a function

Ich verwende Google-Passport-Strategie.

Antwort

0

An der einen Stelle habe ich einen ähnlichen Code gesehen, der Agent wurde innerhalb des before Blocks deklariert.

Sie könnten versuchen:

before(function (done) { 
    agent = superagent.agent(); 
    request 
     .post('/api/login') 
     .send(theAccount) 
     .end(function (err, res) { 
     if (err) { 
      throw err; 
     } 
     agent.saveCookies(res); 
     done(); 
     }); 
    }); 

Referenz: https://github.com/visionmedia/superagent/issues/352