2013-01-15 9 views
9

Próbuję użyć supertest do niektórych testów. Oto fragment kodu, który próbuję testu:Próbujesz użyć supertest, aby sprawdzić ciało odpowiedzi - otrzymywanie błędu

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

Dostaję błąd podczas próby przetestować ciało 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) 

Czy to błąd w supertest, albo ja formatowania mój test jest niepoprawny? Dzięki

+0

marginesie: Pamiętaj, aby obsłużyć błędu w swoim .END funkcja lub zignoruje wszystkie wcześniej zgłoszone wyjątki. – backdesk

Odpowiedz

0

To może być przepisana następująco:

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

Który będzie naprawić ten błąd.

12

Alternatywnie, powinno to działać zbyt:

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

Ponadto, tylko uwaga, ale logicznie myślę, że to ma sens, aby umieścić to w innym wywołaniu expects zamiast w końcowej zwrotnego. Funkcja ta może również zostać ponownie wykorzystane, więc staram się umieścić go gdzieś wielokrotnego użytku, jeśli to możliwe:

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

Teraz można sobie wyobrazić jesteś testowania GET dla /orgs/:orgId i można po prostu ponownie użyć tej samej walidacji.

0

jeśli res.body jest tablicą musisz podać indeks obiektu tak res.body[res.body.length -1].name.should.equal("new_org") - jeśli nieruchomość jest ostatni w szeregu i nie nakazał

Powiązane problemy