浏览器用Jasmine进行IndexedDB测试
我正在考虑使用Jasmine测试indexedDB设置(特别是TestCular)。在一个应用程序中,我正在编写一个数据库,以便在没有任何问题的情况下打开、创建、删除等。现在,当我尝试编写单元测试以确保正确保存服务中的数据时,我不断收到错误,例如超时,并且Chrome资源面板(当前在Chrome 25上测试)不会显示使用正确表创建的数据库。什么是涉及测试indexedDB的简单实现?测试时需要记住几点浏览器用Jasmine进行IndexedDB测试,jasmine,indexeddb,karma-runner,Jasmine,Indexeddb,Karma Runner,我正在考虑使用Jasmine测试indexedDB设置(特别是TestCular)。在一个应用程序中,我正在编写一个数据库,以便在没有任何问题的情况下打开、创建、删除等。现在,当我尝试编写单元测试以确保正确保存服务中的数据时,我不断收到错误,例如超时,并且Chrome资源面板(当前在Chrome 25上测试)不会显示使用正确表创建的数据库。什么是涉及测试indexedDB的简单实现?测试时需要记住几点 您必须记住indexedDB的异步特性,并在Jasmine中适当地处理它。异步测试的关键是使
- 您必须记住indexedDB的异步特性,并在Jasmine中适当地处理它。异步测试的关键是使用jasmine的一些内置函数,如“runs”、“waits”和“waitsFor”。确保在使用这些方法时正确放置它们。(即将waitsFor()放置在beforeach()之外可能会导致超时错误
- 除非您希望自己编写,否则请尝试使用的包装器,并使用最适合您的包装器
- 确保为每次操作创建新事务,否则将收到InvalidStateErrors
- 如果您希望避免在异步测试中使用内置的waitsFor()左右键,您可能希望了解以下内容
describe("Database interaction", function () {
var settings = {
name: "TEST",
version: 1
};
var stores = [
{name: "store1", keyPath: "id"},
{name: "store2", keyPath: "id"},
{name: "store3", keyPath: "id"}
];
var db;
function setupDB(){
var dbRequest = window.indexedDB.open( settings.name, settings.version),
dbDfd = $.Deferred();
dbRequest.onsuccess = function( event ) {
console.log("Opened DB");
db = dbRequest.result;
dbDfd.resolve( db );
};
dbRequest.onblocked = function( event ){
console.error("DB connection blocked");
db.close();
setupDB();
};
dbRequest.onerror = function( event ){
console.error("DB connection issues");
dbDfd.reject();
};
dbRequest.onupgradeneeded = function(){
var i, cur;
db = dbRequest.result;
//Create non-existant tables
for(i=0; i < stores.length; i+=1){
cur = stores[i];
db.createObjectStore( cur.name, {keyPath: cur.keyPath, autoIncrement: true});
}
};
return dbDfd.promise();
}
beforeEach(function(){
var done = false;
runs( function(){
var delRequest = indexedDB.deleteDatabase("TEST");
delRequest.onsuccess = function( event ){
console.log("DB Deleted");
setupDB()
.then(function(db){
console.log("DB Setup with stores: ", db.objectStoreNames );
done = true;
})
};
delRequest.onerror = function(event){
console.log("DB Err: ", event );
done = true;
};
});
waitsFor( function(){ return done; }, "Database never created..", 10000 );
});
it('should add an item to store1', function(){
var done = false;
//Open a transaction with a scope of data stores and a read-write mode.
var trans = db.transaction( stores.map(function(s){return s.name;}), 'readwrite');
//"objectStore()" is an IDBTransaction method that returns an object store
//that has already been added to the scope of the transaction.
var store = trans.objectStore('store1');
var req = store.add({"id": 2, "foo":"bar"});
req.onsuccess = function(){
//Remember to not access store or trans (from above), or this.source.prototype functions as we can only access those in a new transaction
//Added an object to the store, expect result to be ID of item added
expect( this.result ).toBe( 2 );
//Some other expectations
expect( this.source.name ).toBe("store1");
expect( this.source.keyPath ).toBe("id");
expect( this.source.autoIncrement ).toBe( true );
expect( this.source.indexNames.length ).toBe( 0 );
expect( this.transaction.mode ).toBe("readwrite");
expect( this.transaction.db.name ).toBe("TEST");
done = true;
};
req.onerror = function(){
console.log("Error adding object to store");
done = true;
};
waitsFor(function(){ return done; }, "Didn't add store item", 10000 );
});
});
描述(“数据库交互”,函数(){
变量设置={
名称:“测试”,
版本:1
};
变量存储=[
{name:“store1”,keyPath:“id”},
{name:“store2”,keyPath:“id”},
{name:“store3”,keyPath:“id”}
];
var-db;
函数setupDB(){
var dbRequest=window.indexedDB.open(settings.name,settings.version),
dbDfd=$.Deferred();
dbRequest.onsuccess=函数(事件){
控制台日志(“打开的数据库”);
db=dbRequest.result;
dbDfd.resolve(db);
};
dbRequest.onblocked=函数(事件){
控制台错误(“数据库连接被阻止”);
db.close();
setupDB();
};
dbRequest.onerror=函数(事件){
控制台错误(“数据库连接问题”);
拒绝();
};
dbRequest.onupgradeneeded=函数(){
变量i,cur;
db=dbRequest.result;
//创建不存在的表
对于(i=0;i
我有一个相当大的测试套件,其中有一个IDB包装器,它主要依赖于使用等待来执行异步操作。将其与每个之前的和每个之后的相结合非常有用,因为它们可以用于控制数据库连接的设置/减速,如果需要,请查看文件夹查看一些正在运行的功能。请查看。它指的是github pr