2012-11-10 10 views
8

जब मैं परीक्षण चलाता हूं तो मुझे save() विधि में कोई त्रुटि मिलती रहती है।मैं मोचा और मोंगोस के साथ कैसे उपयोग कर सकता हूं?

var User = require('../../models/user') 
, should = require('should'); 

describe('User', function(){ 
    describe('#save()', function(){ 
    it('should save without error', function(done){ 
     var user = new User({ 
     username : 'User1' 
     , email  : '[email protected]' 
     , password : 'foo' 
     }); 
     user.save(function(err, user){ 
     if (err) throw err; 

     it('should have a username', function(done){ 
      user.should.have.property('username', 'User1'); 
      done(); 
     }); 
     }); 
    }) 
    }) 

}) 

यहाँ त्रुटि है:

$ mocha test/unit/user.js 

    ․ 

    ✖ 1 of 1 test failed: 

    1) User #save() should save without error: 
    Error: timeout of 2000ms exceeded 
     at Object.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:1 
61:14) 
     at Timer.list.ontimeout (timers.js:101:19) 
+0

कैसे हम 'वास्तविक समारोह से username' संपत्ति सेट कर सकते हैं ताकि हम' user.should.have में उपयोगकर्ता नाम संपत्ति मिलती है:

तरह से मैं ने लिखा है कि आप क्या हासिल करने की कोशिश कर रहे हैं इस प्रकार है। संपत्ति ('उपयोगकर्ता नाम', 'उपयोगकर्ता 1'); '? क्या हम वास्तविक res में 'res.send ({उपयोगकर्ता नाम:' उपयोगकर्ता 1 '}) 'या' res.render (' home ', {username:' user1 '}) के रूप में भेज सकते हैं? –

उत्तर

7

आप कर सकते हैं घोंसला का वर्णन करता है, लेकिन यह नहीं परीक्षण करती है। प्रत्येक परीक्षण अकेले खड़े होने के लिए होता है ताकि जब आप अपने परिणामों को देख रहे हों तो आप देख सकते हैं कि यह कहां विफल हो रहा है - सहेजने पर या उपयोगकर्ता नाम की संपत्ति नहीं है। उदाहरण के लिए ऊपर दिए गए कोड में 'त्रुटि के बिना सहेजना चाहिए' परीक्षण में असफल होने का कोई तरीका नहीं है क्योंकि कोई नहीं किया गया है()। उपरोक्त कोड का समय भी समाप्त हो रहा है: मोचा को 'त्रुटि के बिना सहेजना चाहिए' परीक्षण के लिए किया गया() नहीं मिल सकता है।

घोंसला वर्णन करने में सक्षम होने के बावजूद बहुत शक्तिशाली है। प्रत्येक वर्णन के भीतर आप प्रत्येक कथन के बाद, पहले, बाद में, पहले से पहले हो सकते हैं। इनके साथ आप उपरोक्त प्रयास कर रहे घोंसले को प्राप्त कर सकते हैं। यदि आप इन बयानों पर पढ़ना चाहते हैं तो अधिक जानकारी के लिए मोचा दस्तावेज़ देखें।

var User = require('../../models/user') 
    , should = require('should') 
    // this allows you to access fakeUser from each test 
    , fakeUser; 

describe('User', function(){ 
    beforeEach(function(done){ 
    fakeUser = { 
     username : 'User1' 
     , email  : '[email protected]' 
     , password : 'foo' 
    } 
    // this cleans out your database before each test 
    User.remove(done); 
    }); 

    describe('#save()', function(){ 
    var user; 
    // you can use beforeEach in each nested describe 
    beforeEach(function(done){ 
     user = new User(fakeUser); 
     done(); 
    } 

    // you are testing for errors when checking for properties 
    // no need for explicit save test 
    it('should have username property', function(done){ 
     user.save(function(err, user){ 
     // dont do this: if (err) throw err; - use a test 
     should.not.exist(err); 
     user.should.have.property('username', 'User1'); 
     done(); 
     }); 
    }); 

    // now try a negative test 
    it('should not save if username is not present', function(done){ 
     user.username = ''; 
     user.save(function(err, user){ 
     should.exist(err); 
     should.not.exist(user.username); 
     done(); 
     }); 
    }); 
    }); 
}); 
+0

जब मैं इस परीक्षण चलाने मैं मिलता है: '\t \t यह (, समारोह (किया) { \t \t ^^ 'उपयोगकर्ता नाम संपत्ति होनी चाहिए' सिंटैक्स त्रुटि: अनपेक्षित identifier' यह पसंद नहीं करता" यह (" ' – chovy

+0

कोई बात नहीं , मुझे बग मिला। धन्यवाद यह बहुत उपयोगी है। – chovy

+0

यदि मैं पहले वर्णन के दूसरे बच्चे के रूप में एक और वर्णन जोड़ता हूं, तो User.rmeove() का कोई प्रभाव नहीं पड़ता है। – chovy

संबंधित मुद्दे

 संबंधित मुद्दे