2016-04-19 12 views
12

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 
+0

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

+0

@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

Trả lời

7

Tại Đơn vị thi của một thể hiện-khởi tạo, bạn không cần phải nhận được thực store dịch vụ. Trong những trường hợp như vậy, thích sử dụng dịch vụ giả. Hành vi của trình khởi tạo thể hiện của bạn là để đặt một số dữ liệu vào cửa hàng được cung cấp bởi ứng dụng. Bạn có thể dễ dàng giả lập cửa hàng đó.

Ví dụ mã kiểm tra với dịch vụ giả:

import Ember from 'ember'; 
import { initialize } from 'preloadtest/instance-initializers/preload'; 
import { module, test } from 'qunit'; 
import destroyApp from '../../helpers/destroy-app'; 

//this is the mock store service: 
const storeStubFactory = Ember.Service.extend({ 
    data: null, 
    init(){ 
    this.data = []; 
    }, 
    pushPayload(payload){ 
     this.get('data').pushObject(payload); 
    }, 
    getAllPayloads(){ 
     return this.get('data'); 
    } 
}); 

module('Unit | Instance Initializer | preload', { 
    beforeEach: function() { 
    Ember.run(() => { 
     this.application = Ember.Application.create(); 
     this.appInstance = this.application.buildInstance(); 
     //Register your mock service (do not create instance, use factory) 
     this.appInstance.register('service:store', storeStubFactory); 
    }); 
    }, 
    afterEach: function() { 
    Ember.run(this.appInstance, 'destroy'); 
    destroyApp(this.application); 
    } 
}); 

// This is your real test: 
test('it works', function(assert) { 
    initialize(this.appInstance); 

    // confirm that mock service has the correct payload:  
    assert.ok(this.appInstance.lookup('service:store').getAllPayloads()); 
}); 

tùy chọn thứ hai

Tất nhiên bạn cũng có thể thử các appInstance paramater của initialize chức năng như sau:

import Ember from 'ember'; 
import { initialize } from 'preloadtest/instance-initializers/preload'; 
import { module, test } from 'qunit'; 
import destroyApp from '../../helpers/destroy-app'; 

const storeStubFactory = Ember.Service.extend({ 
    data: null, 
    init(){ 
    this.data = []; 
    }, 
    pushPayload(payload){ 
     this.get('data').pushObject(payload); 
    }, 
    getAllPayloads(){ 
     return this.get('data'); 
    } 
}); 

module('Unit | Instance Initializer | preload'); 

// This is your real test: 
test('it works', function(assert) { 
    let instance = storeStubFactory.create(); 

    initialize({lookup:function(serviceName){return serviceName==='service:store' ? instance : null;}}); 

    // confirm that mock service has the correct payload: 
    assert.ok(instance.getAllPayloads()); 
}); 

Nhưng Tôi thích sử dụng cái đầu tiên. Chúng tôi đã tuyên bố rằng hành vi khởi tạo thể hiện của bạn là để đặt một số dữ liệu vào cửa hàng được cung cấp bởi ứng dụng. Nhưng trong tùy chọn thứ hai, có vẻ như chúng tôi cũng đang kiểm tra trình khởi tạo thể hiện của bạn là cũng gọi hàm tra cứu của appInstance. Thử nghiệm này được kết hợp nhiều hơn với chi tiết triển khai của bạn.

Các vấn đề liên quan