2012-12-16 13 views
12

Sto cercando di capire dove mettere una funzione per cancellare il database e chiudere la connessione dopo che tutti i test sono stati eseguiti.dove cancellare il database e chiudere la connessione dopo tutti i test usando la moka

Qui sono le mie prove nidificate:

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

Niente sembra funzionare. Ottengo l'errore: timeout di 2000 ms superato

risposta

20

È possibile definire una "radice" after() gancio prima del 1 ° describe() per gestire la pulizia:

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

describe('User', ...); 

Anche se, l'errore che stai ricevendo può essere da 3 asincrona ganci che non informano Mocha per continuare. Questi devono sia chiamare done() o saltare l'argomento in modo che possano essere trattati come sincrono:

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

In realtà, ottengo questo errore il 2 ° tempo di esecuzione make test: '✖ 1 su 5 test fallito: 1) #save utente() "prima di ogni" gancio: Errore: timeout 2000 ms superato ' – chovy

+0

@chovy Ti dà la direzione - '*" prima di ogni "hook *". Quindi hai un 'beforeEach' che non sta finendo, probabilmente perché hai chiamato l'argomento per accettare una richiamata ma quindi non lo chiamano. Con Mocha, devi lasciarlo senza nome (0 argomenti) - 'function() {...}' - o chiamarlo e chiamarlo - 'function (done) {done();} '. –

+0

Ora sto ottenendo un errore diverso: https://gist.github.com/a821 7751061ad6e738b9 1) hook "after all": Errore: timeout di 2000ms superato – chovy

0

ho implementato un po 'diverso.

  1. Ho rimosso tutti i documenti nel gancio "prima" - trovato molto più veloce di dropDatabase().
  2. Ho usato Promise.all() per verificare che tutti i documenti siano stati rimossi prima di uscire dal gancio.

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