2014-02-19 66 views
1

我想確保在Jasmine集成測試後關閉與數據庫的連接。這是我目前的 - 將確保數據庫連接適當關閉嗎?使用Jasmine進行數據庫集成測試

"use strict"; 

describe("MyCtorFunction", function() { 

    describe("myMethod", function() { 
    var _db = null, 
     _testContext = null; 

    beforeEach(function() { 
     _testContext = {}; 
     new DbHelper().openConnection(function (err, dbConnection) { 
     if (err) { 
      throw err; 
     } 

     _db = dbConnection; 
     }); 

     waitsFor(function() { 
     return _db; 
     }, "establishing a connection to the database.", 5000); 
    }); 

    afterEach(function() { 
     waitsFor(function() { 
     return _testContext.assertions.callCount === 1; 
     }, "waiting for the assertions to be called.", 5000); 

     runs(function() { 
     if (_db) { 
      _db.close(); 
      _db = null; 
     } 
     }); 
    }); 

    it("should do something", function() { 
     runs(function() { 
     //arrange 
     _testContext.assertions = assertions; 
     spyOn(_testContext, "assertions").andCallThrough(); 

     //act (_testContext.assertions invoked as callback) 
     new MyCtorFunction(_db).myMethod(_testContext.assertions); 

     //assert 
     function assertions(err, config) { 
      expect(config).toNotBe(null); 
      //etc. 
     } 
     }); 
    }); 
    }); 
}); 

回答

0

不,這是假設如下:

  • 網絡連接測試期間不會丟失
  • 沒有語法或引用錯誤的斷言本身
  • 沒有bug在andCallThroughmyMethod