2

我希望为我为 Ember-Data 编写的自定义适配器编写一些单元测试,但我遇到了错误。这是我设置商店和测试模型的方式:

window.App = Ember.Application.create();
App.Store = DS.Store.extend({ adapter: DS.WebSqlStorageAdapter.extend({ logQueries: true }) });
App.createTables = function() {
  var db = store.adapter.create().db;

  db.transaction(
    function(tx) {
      tx.executeSql('DROP TABLE IF EXISTS test_models;');
      tx.executeSql('CREATE TABLE IF NOT EXISTS test_models (' +
        'id INTEGER PRIMARY KEY AUTOINCREMENT,' +
        '"string" TEXT,' +
        '"number" REAL,' +
        '"date" TEXT,' +
        '"boolean" INTEGER' +
        ');');
    },
    function(err) {
      console.error(err);
      throw new Exception('Database error!');
    },
    function() {
      App.dbCreated = true;
    }
  );

}

App.TestModel = DS.Model.extend({
  number:   DS.attr('number'),
  date:     DS.attr('date'),
  string:   DS.attr('string'),
  boolean:  DS.attr('boolean')
});

App.dbCreated = false;
window.store = App.Store.create();
setTimeout(App.createTables, 500);

这是我的测试设置和我的第一个测试:

var m;

function waitForDbInit() {
  waitsFor(function() { return App.dbCreated; }, 'DB initialization', 4000);
}

function waitsFor(fn, label, time) {
  QUnit.stop();
  var int2 = setInterval(function() {
    throw new Error(label + 'was not completed after ' + time + ' ms.');
  }, time);
  var int =  setInterval(function() {
    if (fn()) {
      clearInterval(int);
      clearInterval(int2);
      QUnit.start();
    }
  }, 50);
}
var inc = 0;
module('CRUD', {
  setup: function() {
    waitForDbInit();
    m = store.createRecord('test_model', {
      id:  ++inc,
      string: 'String!',
      number: 1234,
      date: new Date(),
      boolean: true
    });
  }
});

asyncTest('creates a record', function() {
  m.save().then(function() {
    ok(m.get('number') === 12345);
    start();
  }, function(err) {
    console.error(err);
    ok(false);
    start();
  });
});

当我运行测试时,这是我遇到的每个错误:

Setup failed on retrieves a record: Cannot call method 'lookupFactory' of undefined
Source:
at DS.Store.Ember.Object.extend.modelFor (http://localhost:4567/lib/ember-data.js:2179:34)
at DS.Store.Ember.Object.extend.createRecord (http://localhost:4567/lib/ember-data.js:1343:17)
at Object.module.setup (http://localhost:4567/tests.js:24:15)
at Object.Test.setup (http://localhost:4567/lib/qunit.js:176:31)
at http://localhost:4567/lib/qunit.js:358:10
at process (http://localhost:4567/lib/qunit.js:1453:24)
at http://localhost:4567/lib/qunit.js:479:5
4

0 回答 0