2013-01-15 12 views
9

Sto provando a utilizzare il supertest per alcuni test. Ecco il frammento di codice che sto cercando di prova:Tentativo di utilizzare supertest per controllare il corpo della risposta - ottenendo un errore

it("should create a new org with valid privileges and input with status 201", function(done) { 
    request(app) 
    .post("/orgs") 
    .send({ name: "new_org", owner: "[email protected]", timezone: "America/New_York", currency: "USD"}) 
    .expect(201) 
    .end(function(err, res) { 
     res.body.should.include("new_org"); 
     done(); 
    }); 
}); 

sto ottenendo un errore quando si cerca di testare il corpo res:

TypeError: Object #<Object> has no method 'indexOf' 
    at Object.Assertion.include (../api/node_modules/should/lib/should.js:508:21) 
    at request.post.send.name (../api/test/orgs/routes.js:24:27) 
    at Test.assert (../api/node_modules/supertest/lib/test.js:195:3) 
    at Test.end (../api/node_modules/supertest/lib/test.js:124:10) 
    at Test.Request.callback (../api/node_modules/supertest/node_modules/superagent/lib/node/index.js:575:3) 
    at Test.<anonymous> (../api/node_modules/supertest/node_modules/superagent/lib/node/index.js:133:10) 
    at Test.EventEmitter.emit (events.js:96:17) 
    at IncomingMessage.Request.end (../api/node_modules/supertest/node_modules/superagent/lib/node/index.js:703:12) 
    at IncomingMessage.EventEmitter.emit (events.js:126:20) 
    at IncomingMessage._emitEnd (http.js:366:10) 
    at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23) 
    at Socket.socketOnData [as ondata] (http.js:1367:20) 
    at TCP.onread (net.js:403:27) 

E 'questo un bug in Supertest, o sto formattazione il mio test in modo errato? Grazie

+0

Nota a margine: Ricordati di gestire sbagliare all'interno del vostro .end funzione o ignorerà eventuali eccezioni precedentemente sollevate. – backdesk

risposta

0

Questo può essere riscritta come segue:

res.body.name.should.equal("new_org"); 

Quale sarà correggere l'errore.

12

In alternativa, questo dovrebbe funzionare anche:

res.body.should.have.property("name", "new_org"); 

Inoltre, solo una nota, ma logicamente penso che ha senso mettere questo in un altro chiamata a expects anziché nella richiamata finale. Questa funzione può anche essere riutilizzato, quindi tendo a metterlo da qualche parte riutilizzabile quando possibile:

var isValidOrg = function(res) { 
    res.body.should.have.property("name", "new_org"); 
}; 

it("should create a new org with valid privileges and input with status 201", function(done) { 
    request(app) 
    .post("/orgs") 
    .send({ name: "new_org", owner: "[email protected]", timezone: "America/New_York", currency: "USD"}) 
    .expect(201) 
    .expect(isValidOrg) 
    .end(done); 
}); 

Ora si potrebbe immaginare si sta testando un GET per /orgs/:orgId e si può solo ri-utilizzare lo stesso convalida.

0

se il res.body è un array è necessario fornire l'indice dell'oggetto in modo res.body[res.body.length -1].name.should.equal("new_org") - se la vostra proprietà è l'ultimo nella matrice e non ordinato

Problemi correlati