Yeoman发电机 - 余烬测试设置用于测试余烬数据模型?

时间:2014-01-29 15:54:27

标签: ember.js ember-data yeoman karma-runner yeoman-generator

设置DS.store以测试Ember数据模型的最佳方法是什么?

更新:这是一个小提琴:http://jsfiddle.net/jdcravens/B7Jy6/

我正在使用yeoman余烬生成器,我想为QUnit和Mocha测试的简单入门设置提供指南。

我使用以下方式引导项目:

$ yo ember --karma

到目前为止我无法做的是从我的测试中访问ember-data存储?

首先,我尝试进行类似于ember-data测试套件的设置,将存储设置抽象到初始化程序。

document.write('<div id="ember-testing-container"><div id="ember-testing"></div></div>');
document.write('<style>#ember-testing-container { position: absolute; background: white; bottom: 0; right: 0; width: 800px; height: 500px; overflow: auto; z-index: 9999; border: 5px solid #ccc; } #ember-testing { zoom: 80%; }</style>');

Ember.testing = true;
App.rootElement = "#ember-testing";

App.setupForTesting();
App.injectTestHelpers();

//Ember.run(App, App.advanceReadiness);

// Error: Assertion Failed: You cannot defer readiness since the `ready()` hook 
// has already been called.

window.setupStore = function(options) {
    var env = {};
    options = options || {};

    var container = env.container = new Ember.Container();

    var adapter = env.adapter = (options.adapter || DS.Adapter);
    delete options.adapter;

    for (var prop in options) {
      container.register('model:' + prop, options[prop]);
    }

    container.register('store:main', DS.Store.extend({
      adapter: adapter
    }));

    container.register('serializer:-default', DS.JSONSerializer);
    container.register('serializer:-rest', DS.RESTSerializer);
    container.register('adapter:-rest', DS.RESTAdapter);

    container.injection('serializer', 'store', 'store:main');

    env.serializer = container.lookup('serializer:-default');
    env.restSerializer = container.lookup('serializer:-rest');
    env.store = container.lookup('store:main');
    env.adapter = env.store.get('defaultAdapter');

    return env;
};

window.createStore = function(options) {
    return setupStore(options).store;
};

window.start = function () {};
window.stop = function () {};

然后从我的测试:

/*global describe, it */
'use strict';
(function () {

//var store = App.__container__.lookup('store:main');
//console.log(store);

// LOG: undefined
// PhantomJS 1.9.6 (Mac OS X): Executed 0 of 0 ERROR (0.684 secs / 0 secs)

//So then, I try to generate use createStore() from the initializer.

  var store;

  module("unit/model - DS.Activity", {
    setup: function() {
      store = createStore();
    },

    teardown: function() {
      store = null;
    }
  });

  test('display_id property returns correct value', function() {
    Ember.run(function () {
      var activity = store.push('App.Activity', {'id': 1, 'display_id': 'activity1'});
      var result = activity.get('display_id');
      equal(result, 'activity1', "display_id was " + result);
    });  
  });

// PhantomJS 1.9.6 (Mac OS X) unit/model - DS.Activity display_id property returns
// display_id FAILED
// Died on test #1 at ../node_modules/qunitjs/qunit/qunit.js:42
// at ../test/spec/test.js:58
// at ../test/spec/test.js:60: No model was found for 'App.Activity'
// Error: No model was found for 'App.Activity'
// PhantomJS 1.9.6 (Mac OS X): Executed 1 of 1 (1 FAILED) ERROR (0.743 secs / 0.003 secs)

})();

所以问题是..使用Karma Qunit基本设置访问商店和测试Ember数据模型的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

我已经通过一些更正更新了小提琴:http://jsfiddle.net/jdcravens/B7Jy6/

  module("unit/model - App.Activity", {
    setup: function() {
      store = createStore({activity: App.Activity}); // Pass the model
    },

    teardown: function() {
      store = null;
    }
  });

然后在测试中,注意'活动',而不是'App.Activity':

  test('display_id property returns correct value', function() {
    Ember.run(function () {
      var activity = store.push('activity', {'id': 1, 'display_id': 'activity1'});
      var result = activity.get('display_id');
      equal(result, 'activity1', "display_id was " + result);
    });  
  }); 

我还决定将活动记录的创建移至设置:

  module("unit/model - App.Activity", {
    setup: function() {
      store = createStore({activity: App.Activity}); // Pass the model

      Ember.run(function () {
        activity = store.push('activity', {
          'id': 1,
          'display_id': 'activity1'
        });
      },

    teardown: function() {
      store = null;
    }
  });

然后,在测试中:

  test('display_id property returns correct value', function() {
      var result = activity.get('display_id');
      equal(result, 'activity1', "display_id was " + result);
    });  
  });