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

何も動いていないようにみえ。私はエラーを取得:2000ミリ秒のタイムアウトが

答えて

20

を超えてあなたはクリーンアップを処理するために第一describe()前に「ルート」after()フックを定義することができます。

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

describe('User', ...); 

けれども、あなたが取得しているエラーが3つの非同期からのものであってもよいですMochaに継続を通知していないフック。これらは、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

2000msを超えました – chovy

+0

@ chovyコールバックを受け入れるために引数に名前を付けたのではないかと思いますが、それぞれのフック*の前に '*'あなたはそれを無名(0引数) - 'function(){...}' - または名前をつけてそれを 'function(done){done ();} '。 –

+0

別のエラーが発生しました:https://gist.github.com/a821 7751061ad6e738b9 1) "after all"フック: エラー:2000msのタイムアウトが – chovy

0

私は少し違う、それを実装しました。

  1. "before"フック内のすべてのドキュメントを削除しました。これはdropDatabase()よりもはるかに高速でした。
  2. Promise.all()を使用して、フックを終了する前にすべてのドキュメントが削除されていることを確認しました。 エラー:タイムアウトフック "それぞれの前に" 1)ユーザー#save(): `✖5つのテストの1が失敗しました:実は、私はテストを行い実行している2回目、このエラーが出る

    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(); 
        } 
    }); 
    
関連する問題