Sau khi tạo một ứng dụng ví dụ:Instance kiểm tra đơn vị khởi tạo không thành công với "cửa hàng là undefined"
ember new preloadtest
cd preloadtest/
ember g instance-initializer preload
ember g model test-data
ember g route index
ember g adapter application
Với các tập tin sau đây:
mô hình/thử data.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
value: DS.attr('number')
});
tuyến đường/chỉ mục.js
import Ember from 'ember';
export default Ember.Route.extend({
model(){
return this.store.peekAll('test-data');
}
});
dụ-initializers/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
};
templates/index.hbs
<ul>
{{#each model as |td|}}
<li>{{td.name}}: {{td.value}}</li>
{{/each}}
</ul>
adapter/application.js
import RESTAdapter from 'ember-data/adapters/rest';
export default RESTAdapter.extend({});
ember serve
chạy ứng dụng và hiển thị dữ liệu tải trước nhưng truy cập /tests
kiểm tra đơn vị mặc định cho trình khởi chạy phiên bản preload
không thành công với lỗi store is undefined
.
Full Thông báo lỗi:
Died on test #1 @http://localhost:4200/assets/tests.js:212:1
[email protected]://localhost:4200/assets/vendor.js:94:20
[email protected]://localhost:4200/assets/vendor.js:142:5
[email protected]://localhost:4200/assets/vendor.js:193:5
[email protected]://localhost:4200/assets/vendor.js:181:12
[email protected]://localhost:4200/assets/test-loader.js:67:9
[email protected]://localhost:4200/assets/test-loader.js:58:13
[email protected]://localhost:4200/assets/test-loader.js:89:7
@http://localhost:4200/assets/test-support.js:6397:5
: store is [email protected] 114 ms
Source:
[email protected]://localhost:4200/assets/preloadtest.js:213:5
@http://localhost:4200/assets/tests.js:213:1
[email protected]://localhost:4200/assets/test-support.js:2716:14
[email protected]://localhost:4200/assets/test-support.js:2701:4
run/<@http://localhost:4200/assets/test-support.js:2843:6
[email protected]://localhost:4200/assets/test-support.js:2502:4
[email protected]://localhost:4200/assets/test-support.js:2484:2
resumeProcessing/<@http://localhost:4200/assets/test-support.js:2544:4
Làm thế nào để khởi tạo cửa hàng của ứng dụng để nó có thể được sử dụng trong các thử nghiệm đơn vị?
Edit - kiểm tra/đơn vị/Ví dụ-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);
});
thử nó với needs: [ 'service:store' ]
và không có (mặc dù đề nghị của nó mà bạn should not need to do this nếu Ember-Data là trên trang - mà tôi cũng đã thử nhập cả trong bài kiểm tra đơn vị và trong trình khởi tạo cá thể).
phiên bản:
Ember : 2.4.5
Ember Data : 2.5.2
Bạn đã thử nghiệm để xác định 'dịch vụ: lưu trữ' làm phụ thuộc trong kiểm tra đơn vị của bạn thông qua' nhu cầu '? – jelhan
@jelhan Có, đã thử nó với 'nhu cầu: [' dịch vụ: lưu trữ '] 'và (như [theo gợi ý ở đây] (https://github.com/emberjs/data/issues/2994#issuecomment-99615466)) đã thử nhập 'ember-data' và đã cố gắng tìm ra cách tạo cửa hàng trên cá thể mới nhưng không thành công. – MT0