Javascript 如何使用Linq2IndexedDB插入对象

Javascript 如何使用Linq2IndexedDB插入对象,javascript,unit-testing,mocha.js,karma-runner,linq2indexeddb,Javascript,Unit Testing,Mocha.js,Karma Runner,Linq2indexeddb,我正在通过单元测试(via)试验Linq2IndexedDB(1.0.21版),但我甚至不能让一个简单的插入工作。发生的情况(在Google Chrome下运行时)是在Linq2IndexedDB.js的第1535行抛出一个内部异常: 未捕获类型错误:无法读取未定义的属性“版本” 我的单元测试代码如下所示;基本上有一个测试,“它可以添加对象”: 我是做错了什么,还是Linq2IndexedDB中有bug(或者两者都有) 我已经建立了一个相应的测试项目,并完成了配置,因此您可以轻松地运行包含的测试

我正在通过单元测试(via)试验Linq2IndexedDB(1.0.21版),但我甚至不能让一个简单的插入工作。发生的情况(在Google Chrome下运行时)是在Linq2IndexedDB.js的第1535行抛出一个内部异常:

未捕获类型错误:无法读取未定义的属性“版本”

我的单元测试代码如下所示;基本上有一个测试,“它可以添加对象”:

我是做错了什么,还是Linq2IndexedDB中有bug(或者两者都有)


我已经建立了一个相应的测试项目,并完成了配置,因此您可以轻松地运行包含的测试。Karma配置假定您安装了Chrome。

我发现了几个问题:

  • 我获取的LinqIndexedDB工作程序位置错误,应该是:
    '/base/lib/linqIndexedDB.js'
  • 使用线外关键点插入对象不起作用
  • 我最终在IE 10和Chrome上完成了插入工作,尽管我仍在与PhantomJS作斗争。为了让它在Chrome下工作,我必须显式地指定我的模式,我怀疑这是由于Linq2IndexedDB中的一个bug造成的。我的工作方案如下:

    测试: 实施: 编辑: 它不能在PhantomJS下工作的原因是我在Linq2IndexedDB中启用了调试日志记录,出于某种原因,这似乎会在某个时候阻塞Karma的管道。关闭调试日志记录后,所有配置都可以工作

    "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"));
            });
        });
    });
    
    "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) {
                console.log("err:", 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) {
                // Linq2IndexedDB's web worker needs this URL
                linq2indexeddb.prototype.utilities.linq2indexedDBWorkerFileLocation = '/base/lib/Linq2IndexedDB.js'
    
                _db = new db.Database("test");
    
                console.log("Deleting database");
                _db.deleteDatabase()
                .done(function () {
                    console.log("Initializing database");
                    _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.insert(refObj)
                .done(function () {
                    done();
                })
                .fail(bindFail(done, "Database insertion failed"));
            });
        });
    });
    
    define("db", ["linq2indexeddb"], function (linq2indexeddb) {
        function getDatabaseConfiguration() {
            var dbConfig = {
                version: 1
            };
            // NOTE: definition is an array of schemas, keyed by version;
            // this allows linq2indexedDb to do auto-schema-migrations, based upon the current dbConfig.version
            dbConfig.definition = [{
                version: 1,
                objectStores: [
                { name: "store", objectStoreOptions: { keyPath: 'key' } },
                ],
                defaultData: []
            },
            ];
    
            return dbConfig;
        }
    
        var module = {
            Database: function (name) {
                var self = this;
    
                self._db = linq2indexeddb(name, getDatabaseConfiguration());
    
                self.deleteDatabase = function () {
                    return self._db.deleteDatabase();
                };
    
                self.initialize = function () {
                    return self._db.initialize();
                };
    
                self.insert = function (data) {
                    return self._db.linq.from("store").insert(data);
                };
            }
        };
        return module;
    });