0

単体テスト ( Mocha経由) を介して Linq2IndexedDB (v. 1.0.21) を試していますが、単純な挿入作業を行うことさえできません。(Google Chrome で実行している場合) Linq2IndexedDB.js の 1535 行目で内部例外がスローされます。

Uncaught TypeError: Cannot read property 'version' of undefined

私の単体テストコードは次のようになります。基本的に、「オブジェクトを追加できる」というテストが 1 つあります。

"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 がインストールされていることを前提としています。

4

1 に答える 1

0

私はいくつかの問題を見つけました:

  1. Linq2IndexedDB ワーカーの場所が間違っています。次のようにする必要があります。'/base/lib/Linq2IndexedDB.js'
  2. 行外のキーを持つオブジェクトの挿入は機能しません。

PhantomJS にはまだ苦労していますが、最終的には IE 10 と Chrome で挿入が機能するようになりました。Chrome で動作させるには、スキーマを明示的に指定する必要がありました。これは、Linq2IndexedDB のバグによるものと思われます。私の作業ソリューションは次のとおりです。

テスト:

"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;
});

編集: PhantomJS で動作しなかった理由は、Linq2IndexedDB でデバッグ ログを有効にしたためでした。何らかの理由で、ある時点で Karma のパイプが詰まるように見えました。デバッグ ログをオフにすると、すべての構成が機能します。

于 2013-04-21T21:45:08.193 に答える