Ember 需要模型的测试初始化程序
Ember Test Initializer that Requires Model
我有一个初始化程序,它通过脚本标记中页面上的 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
};
tests/initializers/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
字段在这种情况下似乎不起作用。或者有什么办法可以使初始化程序更易于测试。
此答案归功于 https://github.com/taras。
我们可以创建验收测试来断言属性已正确注入到我们的容器中,而不是为此初始化程序创建单元测试。
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
我有一个初始化程序,它通过脚本标记中页面上的 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
};
tests/initializers/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
字段在这种情况下似乎不起作用。或者有什么办法可以使初始化程序更易于测试。
此答案归功于 https://github.com/taras。
我们可以创建验收测试来断言属性已正确注入到我们的容器中,而不是为此初始化程序创建单元测试。
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