javascript - 需要模型的 Ember 测试初始化​​器

标签 javascript unit-testing ember.js ember-cli ember-qunit

我有一个初始化程序,它通过脚本标记中页面上的 JSON 对象向应用程序注册一些模块。在应用程序中工作正常,但测试失败,因为它找不到预期的模型。

initialzers/bootstrap-payload.js

export function initialize(container, application) {
  var store = container.lookup('service:store'),
    payloadKeys = Object.keys(BOOTSTRAP_DATA);

  payloadKeys.forEach((key) => {
    var registryKey = `bootstrap-payload:${key}`,
        model;

    model = store.createRecord(key, BOOTSTRAP_DATA[key]);
    application.register(registryKey, model, {instantiate:false});
  });
}

export default {
  name: 'bootstrap-payload',
  after: 'ember-data',
  initialize: initialize
};

测试/初始化程序/bootstrap-payload-test.js

import Ember from 'ember';
import { initialize } from '../../../initializers/bootstrap-payload';
import { module, test } from 'qunit';

var registry, application;

module('Unit | Initializer | bootstrap payload', {
  needs: ['model:channel'],

  beforeEach: function() {
    Ember.run(function() {
      application = Ember.Application.create();
      registry = application.registry;
      application.deferReadiness();
    });
  }
});

// Replace this with your real tests.
test('it works', function(assert) {
  initialize(registry, application);

  // you would normally confirm the results of the initializer here
  assert.ok(true);
});

tests/index.html 包含一个示例 BOOTSTRAP_DATA 变量,其中包含一个模型,该模型始终被期望存在,称为 channel。运行 ember test 时出现以下错误。

at http://localhost:7357/assets/test-support.js:5604: No model was found for 'channel'

在这种情况下,我如何才能将此依赖项注入(inject) needs 字段似乎不起作用。或者有什么办法可以使初始化程序更易于测试。

最佳答案

归功于 https://github.com/taras对于这个答案。

我们可以创建一个验收测试来断言属性已正确注入(inject)到我们的容器中,而不是为此初始化程序创建单元测试。

import Ember from 'ember';
import { module, test } from 'qunit';
import startApp from 'test-models-in-initializer/tests/helpers/start-app';
import Channel from 'test-models-in-initializer/models/channel';

var application;

module('Acceptance | index', {
  beforeEach: function() {
    window.BOOTSTRAP_DATA = {
      'channel': {
        'id': 0,
        'name': 'Test Channel',
        'internalName': 'test-channel',
        'logoUrl': '//somecdn.net/test-channel/logo.png'
      }
    };
    application = startApp();
  },

  afterEach: function() {
    Ember.run(application, 'destroy');
  }
});

test('channel type', function(assert) {
  let channel = application.registry.lookup('bootstrap-payload:channel');
  assert.ok(channel, "is registered");
  assert.ok(channel instanceof Channel);
});

在此处测试应用程序 https://github.com/embersherpa/test-models-in-initializer

关于javascript - 需要模型的 Ember 测试初始化​​器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31437916/

相关文章:

javascript - 如何使用错误子状态在 Ember.js 中显示整页错误?

javascript - 使用 js/jquery 从 csv 创建谷歌图表

javascript - getElementsByTagName 返回 null 值(但在 Debug模式下工作得异常正常)

javascript - 从Reducer调用选择器

angularjs - 使用 Angular 2 模拟组件

python - 如何模拟属于库一部分的父类(super class)?

javascript - Ember 路由和链接结构

javascript - 如何使用 srcset 延迟加载图像?

python - 在 python 中将参数从 Suite 传递给 TestCase

ember.js - 如何分页 Ember 数据关系