我正在研究使用Jasmine(特别是Testacular)测试indexedDB设置。在应用程序内部我正在编写要打开,创建,删除等的数据库,没有任何问题。现在,当我尝试编写单元测试以确保正确地从服务中保存数据时,我不断收到错误,例如超时,Chrome资源面板(目前在Chrome 25上测试)不会显示使用正确的表格。什么是涉及测试indexedDB的简单实现?
答案 0 :(得分:7)
在测试indexedDB时,请注意以下几点。
以下是我用来演示将一个项目添加到数据库的简单测试。这应该适用于Chrome 24 +,FF 16+和IE10(授予您使用Deferred的jquery版本),不含供应商前缀。但我强烈建议使用上述IDB包装器/插件之一。这里可能需要添加更多错误输出,但希望这有助于入门。
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 :(得分:1)
我在db.js中有一个相当大的测试套件,它是一个IDB包装器,它主要依赖于使用waitsFor
来执行异步操作。将其与beforeEach
和afterEach
结合使用非常有用,因为它们可用于控制数据库连接的设置/删除,如果要查看某些内容,请查看specs文件夹行动。
答案 2 :(得分:0)
看看this。它引用了一个github项目,它有一个在jasmine中使用indexeddb的简单例子。