AngularJS 数据库服务的单元测试
AngularJS unit tests for database service
我想为我的 angularjs 应用程序添加一些专门针对数据库服务的测试用例。
好吧,问题是我无法在单元测试中真正打开数据库。
我的测试用例如下所示:
describe("DatabaseCreateService‚", function () {
var DatabaseCreateService,cordovaSQLite;
beforeEach(module("starter.services"));
beforeEach(module("ngCordova"));
beforeEach(module("ionic"));
beforeEach(inject(function (_DatabaseCreateService_, $cordovaSQLite) {
DatabaseCreateService = _DatabaseCreateService_;
cordovaSQLite = $cordovaSQLite;
}));
it("should create the table cg_local_values", function () {
var db = DatabaseCreateService.initDB("Test.db");
expect(db).toBeDefined();
});
});
我的工厂是这样的:
var initDB = function(dbName) {
$ionicPlatform.ready(function() {
if (window.cordova) {
db = $cordovaSQLite.openDB(dbName);
return db;
}else{
db = window.openDatabase(dbName, '1', 'my', 1024 * 1024 * 100);
console.log(JSON.stringify(db));
return db;
}
});
};
好吧,如果我 运行 karma start:
Expected undefined to be defined.
at Object.<anonymous> (pathToTests/tests/services/DatabaseServiceTest.js:42:16)
Chrome 46.0.2490 (Mac OS X 10.11.1): Executed 24 of 24 (1 FAILED) (0.183 secs / 0.146 secs)
作为业力测试浏览器,我使用Chrome浏览器。
好吧,我发现我必须在 ionicPlatform 准备好后将数据库保存在 beforeEach 中。现在它看起来像这样并且它正在工作:
var DatabaseCreateService、cordovaSQLite、ionicPlatform、rootScope、q;
var db=null;
beforeEach(module("starter.services"));
beforeEach(module("ngCordova"));
beforeEach(module("ionic"));
beforeEach(inject(function (_DatabaseCreateService_, $cordovaSQLite,$ionicPlatform,$rootScope,$q) {
DatabaseCreateService = _DatabaseCreateService_;
cordovaSQLite = $cordovaSQLite;
ionicPlatform = $ionicPlatform;
q = $q;
rootScope = $rootScope;
ionicPlatform.ready(function() {
db = window.openDatabase("Test.db", '1', 'my', 1024 * 1024 * 100);
});
}));
我想为我的 angularjs 应用程序添加一些专门针对数据库服务的测试用例。
好吧,问题是我无法在单元测试中真正打开数据库。
我的测试用例如下所示:
describe("DatabaseCreateService‚", function () {
var DatabaseCreateService,cordovaSQLite;
beforeEach(module("starter.services"));
beforeEach(module("ngCordova"));
beforeEach(module("ionic"));
beforeEach(inject(function (_DatabaseCreateService_, $cordovaSQLite) {
DatabaseCreateService = _DatabaseCreateService_;
cordovaSQLite = $cordovaSQLite;
}));
it("should create the table cg_local_values", function () {
var db = DatabaseCreateService.initDB("Test.db");
expect(db).toBeDefined();
});
});
我的工厂是这样的:
var initDB = function(dbName) {
$ionicPlatform.ready(function() {
if (window.cordova) {
db = $cordovaSQLite.openDB(dbName);
return db;
}else{
db = window.openDatabase(dbName, '1', 'my', 1024 * 1024 * 100);
console.log(JSON.stringify(db));
return db;
}
});
};
好吧,如果我 运行 karma start:
Expected undefined to be defined.
at Object.<anonymous> (pathToTests/tests/services/DatabaseServiceTest.js:42:16)
Chrome 46.0.2490 (Mac OS X 10.11.1): Executed 24 of 24 (1 FAILED) (0.183 secs / 0.146 secs)
作为业力测试浏览器,我使用Chrome浏览器。
好吧,我发现我必须在 ionicPlatform 准备好后将数据库保存在 beforeEach 中。现在它看起来像这样并且它正在工作:
var DatabaseCreateService、cordovaSQLite、ionicPlatform、rootScope、q; var db=null;
beforeEach(module("starter.services"));
beforeEach(module("ngCordova"));
beforeEach(module("ionic"));
beforeEach(inject(function (_DatabaseCreateService_, $cordovaSQLite,$ionicPlatform,$rootScope,$q) {
DatabaseCreateService = _DatabaseCreateService_;
cordovaSQLite = $cordovaSQLite;
ionicPlatform = $ionicPlatform;
q = $q;
rootScope = $rootScope;
ionicPlatform.ready(function() {
db = window.openDatabase("Test.db", '1', 'my', 1024 * 1024 * 100);
});
}));