mocha

2012-12-16 16 views
12

का उपयोग करके सभी परीक्षणों के बाद डेटाबेस और बंद कनेक्शन को हटाने के लिए, मैं यह पता लगाने की कोशिश कर रहा हूं कि डेटाबेस को हटाने के लिए फ़ंक्शन कहां रखना है और सभी परीक्षणों के बाद कनेक्शन बंद करना है।mocha

यहाँ मेरी नेस्टेड परीक्षण कर रहे हैं:

//db.connection.db.dropDatabase(); 
//db.connection.close(); 

describe('User', function(){ 
    beforeEach(function(done){ 
    }); 

    after(function(done){ 
    }); 

    describe('#save()', function(){ 
     beforeEach(function(done){ 
     }); 

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

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

    describe('#find()', function(){ 
     beforeEach(function(done){ 
      user.save(function(err, user){ 
       done(); 
      }); 
     }); 

     it('should find user by email', function(done){ 
      User.findOne({email: fakeUser.email}, function(err, user){ 
       done(); 
      }); 
     }); 

     it('should find user by username', function(done){ 
      User.findOne({username: fakeUser.username}, function(err, user){ 
       done(); 
      }); 
     }); 
    }); 
}); 

कुछ भी नहीं काम करने के लिए लगता है। मैं त्रुटि मिलती है: 2000ms की टाइमआउट को पार कर

उत्तर

20

आप "मूल" 1 describe() से पहले after() हुक सफाई को संभालने के लिए परिभाषित कर सकते हैं:

after(function (done) { 
    db.connection.db.dropDatabase(function() { 
     db.connection.close(function() { 
      done(); 
     }); 
    }); 
}); 

describe('User', ...); 

हालांकि, त्रुटि आप 3 अतुल्यकालिक से हो सकता है हो रही है हुक जो जारी रखने के लिए मोचा को सूचित नहीं कर रहे हैं। ये या तो done() कॉल या तर्क को छोड़ने के लिए तो वे तुल्यकालिक के रूप में इलाज किया जा सकता की जरूरत है:

describe('User', function(){ 
    beforeEach(function(done){ // arg = asynchronous 
     done(); 
    }); 

    after(function(done){ 
     done() 
    }); 

    describe('#save()', function(){ 
     beforeEach(function(){ // no arg = synchronous 
     }); 

     // ... 
    }); 
}); 

From the docs:

By adding a callback (usually named done) to it() Mocha will know that it should wait for completion.

+0

वास्तव में, मैं इस त्रुटि 2 बार परीक्षण कर चलाने के: '✖ 5 में से 1 परीक्षण असफल: 1) उपयोगकर्ता #save() हुक" प्रत्येक से पहले ": त्रुटि: का समय समाप्त 2000ms ' – chovy

+0

@chovy से अधिक है यह आपको प्रत्येक "हुक *" से पहले' * 'दिशा दे रहा है। इसलिए, आपके पास' पहले से 'है जो परिष्करण नहीं कर रहा है, शायद इसलिए कि आपने कॉलबैक स्वीकार करने के लिए तर्क का नाम दिया है लेकिन तो इसे कॉल नहीं कर रहे हैं। मोचा के साथ, आपको या तो इसे अनाम (0 तर्क) - 'फ़ंक्शन() {...} '- या इसे नाम दें और इसे कॉल करें -' फ़ंक्शन (किया गया) {किया गया();} ' –

+0

अब मुझे एक अलग त्रुटि मिल रही है: https://gist.github.com/a821 7751061ad6e738b9 1) "सब के बाद" हुक: त्रुटि: 2000ms का टाइमआउट – chovy

0

मैं यह थोड़ा अलग कार्यान्वित किया।

  1. मैंने "पहले" हुक में सभी दस्तावेज़ हटा दिए - इसे ड्रॉपडाबेस() से बहुत तेज पाया।
  2. मैंने यह सुनिश्चित करने के लिए Promise.all() का उपयोग किया था कि हुक से बाहर निकलने से पहले सभी दस्तावेज़ हटा दिए गए थे।

    beforeEach(function (done) { 
    
        function clearDB() { 
         var promises = [ 
          Model1.remove().exec(), 
          Model2.remove().exec(), 
          Model3.remove().exec() 
         ]; 
    
         Promise.all(promises) 
          .then(function() { 
           done(); 
          }) 
        } 
    
        if (mongoose.connection.readyState === 0) { 
         mongoose.connect(config.dbUrl, function (err) { 
          if (err) { 
           throw err; 
          } 
          return clearDB(); 
         }); 
        } else { 
         return clearDB(); 
        } 
    });