2

我想为我的angularjs应用程序专门为数据库服务添加一些测试用例。针对数据库服务的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; 
    } 
    }); 
}; 

好吧,我,如果我跑因缘开始得到这个错误:

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浏览器。

回答

1

那么我发现,我必须在离子平台准备好之前将数据库保存在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); 
    }); 
}));