Hello I want to implement some test cases for my ionic framework application which uses cordova sqliteplugin to get data from a sqlite database.
I'm very new in writing test cases for angularjs.
Well in my app.js I open the database with this:
angular.module('starter', ['ionic','starter.services', 'starter.controllers', 'starter.config','ngCordova'])
.run(function($ionicPlatform, $cordovaSQLite, $ionicLoading) {
$ionicPlatform.ready(function() {
var dbName = 'testDB.db';
if(window.cordova) {
window.plugins.sqlDB.copy(dbName);
db = $cordovaSQLite.openDB(dbName);
}
....
My services.js are looking like this:
angular.module('starter.services', ['ngCordova'])
.factory('ProductService',function($cordovaSQLite){
var getAllProducts = function() {
var productList = [];
var query = "SELECT c.name as name, c.id as id FROM cars as c ORDER BY c.name";
$cordovaSQLite.execute(db, query, []).then(function(res) {
if(res.rows.length > 0) {
for(var i = 0; i < res.rows.length; i++) {
productList.push({id: res.rows.item(i).id, name: res.rows.item(i).name});
}
}
}, function (err) {
console.error(err);
});
return productList;
};
return {
getAllProducts: getAllProducts
};
});
And my test description to test the service looks like this:
describe('ProductService unit tests', function () {
var ProductService;
beforeEach(module('starter.services'));
beforeEach(angular.mock.module('starter'));
beforeEach(inject(function(_ProductService_) {
ProductService = _ProductService_;
}));
it('can get an instance', inject(function(ProductService) {
expect(ProductService).toBeDefined();
}));
//THIS TEST FAILS
it('should get some products from the database', inject(function(ProductService) {
expect(ProductService.getAllProducts().length).toBeGreaterThan(0);
}));
});
If I run the test I get:
TypeError: 'null' is not an object (evaluating 'n.transaction')
I think its because the database is not really initialized during the test, so I tried to mock the starter app but it didn't solve the problem. I tried to google on this but could not find any examples or tutorials for karma with sqlite and angularjs.
Thanks for any help.
Aucun commentaire:
Enregistrer un commentaire