我有一个初始化程序,它从脚本标记中页面上的 JSON 对象向应用程序注册一些模块。在应用程序中工作正常,但测试失败,因为它找不到预期的模型。
初始化程序/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'
在这种情况下,如何注入此依赖项该needs
字段似乎不起作用。或者无论如何使初始化程序更可测试。