我正在通过单元测试(通过Mocha)尝试 Linq2IndexedDB(v. 1.0.21),但我什至无法进行简单的插入工作。发生的情况(在 Google Chrome 下运行时)是在 Linq2IndexedDB.js 的第 1535 行引发内部异常:
Uncaught TypeError: Cannot read property 'version' of undefined
我的单元测试代码如下所示;基本上有一个测试,“它可以添加对象”:
"use strict";
define(["db", "linq2indexeddb", "chai", "underscore", "stacktrace"], function (db, linq2indexeddb, chai, _,
printStacktrace) {
var should = chai.should();
describe("db", function () {
var _db;
function fail(done, reason, err) {
if (typeof reason === "string") {
reason = new Error(reason);
}
if (!reason) {
console.log(typeof done, typeof reason);
reason = new Error("There's been an error, but no reason was supplied!");
var st = printStacktrace({e: reason});
console.log(st);
}
if (typeof done !== "function") {
throw new Error("Was not supplied a function for 'done'!");
}
done(reason);
}
// Bind done as the first argument to the fail function
function bindFail(done, reason) {
if (typeof done !== "function") {
throw new Error("done must be a function");
}
return _.partial(fail, done, reason);
}
beforeEach(function (done) {
_db = linq2indexeddb("test", null, true);
_db.deleteDatabase()
.done(function () {
_db.initialize()
.done(done)
.fail(bindFail(done, "Initializing database failed"));
})
.fail(bindFail(done, "Deleting database failed"));
});
it("can add objects", function (done) {
console.log("Starting test");
var refObj = {"key": "value"};
_db.linq.from("store").insert(refObj, "Key")
.done(function () {
console.log("Added object successfully");
done();
})
.fail(bindFail(done, "Inserting object failed"));
});
});
});
我在这里做错了什么,还是 Linq2IndexedDB(或两者)中存在错误?
我在 Github 上建立了一个相应的测试项目,并配有Karma配置,因此您可以轻松运行包含的测试。Karma 配置假定您已安装 Chrome。