生成示例应用程序后:
ember new preloadtest
cd preloadtest/
ember g instance-initializer preload
ember g model test-data
ember g route index
ember g adapter application
使用以下文件:
模型/测试数据.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
value: DS.attr( 'number' )
});
路线/index.js
import Ember from 'ember';
export default Ember.Route.extend({
model(){
return this.store.peekAll( 'test-data' );
}
});
实例初始化器/preload.js
export function initialize( appInstance ) {
let store = appInstance.lookup( 'service:store' );
store.pushPayload( { "testDatas": [
{ "id": 1, "name": "aaa", "value": 1},
{ "id": 2, "name": "bbb", "value": 2},
{ "id": 3, "name": "ccc", "value": 3}
] } );
}
export default {
name: 'preload',
initialize
};
模板/index.hbs
<ul>
{{#each model as |td|}}
<li>{{td.name}}: {{td.value}}</li>
{{/each}}
</ul>
适配器/application.js
import RESTAdapter from 'ember-data/adapters/rest';
export default RESTAdapter.extend({});
ember serve
运行应用程序并显示预加载数据,但进入实例初始化程序/tests
的默认单元测试失败并出现错误。preload
store is undefined
完整的错误信息:
Died on test #1 @http://localhost:4200/assets/tests.js:212:1
Module.prototype.exports@http://localhost:4200/assets/vendor.js:94:20
Module.prototype.build@http://localhost:4200/assets/vendor.js:142:5
findModule@http://localhost:4200/assets/vendor.js:193:5
requireModule@http://localhost:4200/assets/vendor.js:181:12
TestLoader.prototype.require@http://localhost:4200/assets/test-loader.js:67:9
TestLoader.prototype.loadModules@http://localhost:4200/assets/test-loader.js:58:13
TestLoader.load@http://localhost:4200/assets/test-loader.js:89:7
@http://localhost:4200/assets/test-support.js:6397:5
: store is undefined@ 114 ms
Source:
initialize@http://localhost:4200/assets/preloadtest.js:213:5
@http://localhost:4200/assets/tests.js:213:1
runTest@http://localhost:4200/assets/test-support.js:2716:14
Test.prototype.run@http://localhost:4200/assets/test-support.js:2701:4
run/<@http://localhost:4200/assets/test-support.js:2843:6
process@http://localhost:4200/assets/test-support.js:2502:4
begin@http://localhost:4200/assets/test-support.js:2484:2
resumeProcessing/<@http://localhost:4200/assets/test-support.js:2544:4
如何初始化应用程序的存储以便它可以在单元测试中使用?
编辑 - tests/unit/instance-initializers/preload-test.js
import Ember from 'ember';
import { initialize } from 'preloadtest/instance-initializers/preload';
import { module, test } from 'qunit';
import destroyApp from '../../helpers/destroy-app';
//import DS from 'ember-data';
module('Unit | Instance Initializer | preload', {
//needs: [ 'service:store' ],
beforeEach: function() {
Ember.run(() => {
this.application = Ember.Application.create();
this.appInstance = this.application.buildInstance();
});
},
afterEach: function() {
Ember.run(this.appInstance, 'destroy');
destroyApp(this.application);
}
});
// Replace this with your real tests.
test('it works', function(assert) {
initialize(this.appInstance);
// you would normally confirm the results of the initializer here
assert.ok(true);
});
有无尝试needs: [ 'service:store' ]
(尽管它建议如果 Ember-Data 在页面上,您不需要这样做- 我也尝试在单元测试和实例初始化程序中导入)。
版本:
Ember : 2.4.5
Ember Data : 2.5.2