2013-01-15 14 views
9

मैं कुछ परीक्षण के लिए सबसे अच्छा उपयोग करने की कोशिश कर रहा हूं।प्रतिक्रिया के शरीर की जांच करने के लिए सुपरटेस्ट का उपयोग करने की कोशिश कर रहा है - एक त्रुटि प्राप्त करना

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

मैं जब रेस शरीर का परीक्षण करने के प्रयास में कोई त्रुटि हो रही है:

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) 

Supertest में एक बग यह है, या मैं स्वरूपण कर रहा हूँ यहाँ कोड का टुकड़ा है कि मैं परीक्षण करने के लिए कोशिश कर रहा हूँ है मेरा परीक्षण गलत तरीके से?

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

कौन-सा त्रुटि ठीक कर देंगे: धन्यवाद

+0

साइड टिप्पणी है: अपने .end भीतर गलती को संभालने के लिए याद रखें कार्य या यह पहले उठाए गए अपवादों को अनदेखा कर देगा। – backdesk

उत्तर

0

यह इस प्रकार के रूप में लिखा जा सकता है। इसके अलावा

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

, बस एक नोट लेकिन तार्किक मुझे लगता है कि यह समझ में आता है expects के बजाय अंतिम कॉलबैक में किसी दूसरी कॉल में यह डाल करने के लिए:

12

वैकल्पिक रूप से, यह भी काम करना चाहिए। यह समारोह भी फिर से इस्तेमाल किया जा सकता है, इसलिए मैं जब भी संभव हो इसे कहीं पुन: प्रयोज्य डाल करने के लिए करते हैं:

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

अब आप /orgs/:orgId के लिए एक GET परीक्षण कर रहे हैं कल्पना कर सकते हैं और आप सिर्फ एक ही मान्यता फिर से इस्तेमाल कर सकते हैं।

0

अगर आपके res.body एक सरणी तुम इतनी res.body[res.body.length -1].name.should.equal("new_org") वस्तु का सूचकांक प्रदान करने की आवश्यकता है - अगर अपनी संपत्ति सरणी में पिछले और आदेश दिया नहीं