Closed remisture closed 6 years ago
I have the same problem!
Sorry, I did not see this earlier. I will take a look. Thanks for supplying the sample code.
I have the same problem this is my code:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
/*
Generated class for the DatabaseProvider provider.
See https://angular.io/guide/dependency-injection for more info on providers
and Angular DI.
*/
@Injectable()
export class DatabaseProvider {
private db: SQLiteObject;
private isOpen: boolean;
constructor(
public http: Http,
public storage: SQLite
) {
if (!this.isOpen) {
this.storage = new SQLite();
this.storage.create({ name: "data.db", location: "default" }).then((db: SQLiteObject) => {
this.db = db;
db.executeSql("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, identification INTEGER, name TEXT, lastname text)", []);
this.isOpen = true;
console.log("DatabaseProvider db open");
}).catch((error) => {
console.error("DatabaseProvider db open fail");
console.log(error);
})
}
}
CreateUser(identification: number, name:string, lastname:string){
return new Promise ((resolve, reject) => {
let sql = "INSERT INTO users (identification, name, lastname) VALUES (?, ?, ?)";
this.db.executeSql(sql, [identification, name, lastname]).then((data) =>{
resolve(data);
}, (error) => {
reject(error);
});
});
}
GetAllUsers(){
return new Promise ((resolve, reject) => {
this.db.executeSql("SELECT * FROM users", []).then((data) => {
let arrayUsers = [];
if (data.rows.length > 0) {
for (var i = 0; i < data.rows.length; i++) {
arrayUsers.push({
id: data.rows.item(i).id,
identification: data.rows.item(i).identification,
name: data.rows.item(i).name,
lastname: data.rows.item(i).lastname
});
}
}
resolve(arrayUsers);
}, (error) => {
reject(error);
})
})
}
}
the problem is on creation of db, i see this log:
DatabaseProvider db open fail cordova_not_available
When I created this mock, it was a simple port of the method calls. Because this is a complex plugin, involving db access, it is hard to write a functioning mock that will replicate the suite of abilities. I am afraid this is one plugin that you will not be able to use mocks for.
Working code without mock class