This integration test involves database access and hence opening and closing a database connection. Does it look correct? I am concerned about ensuring the the database connection is closed should the test fail.
"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.
}
});
});
});
});