14

サンプル アプリケーションの生成後:

ember new preloadtest
cd preloadtest/
ember g instance-initializer preload
ember g model test-data
ember g route index
ember g adapter application

次のファイルを使用します。

models/test-data.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' );
  }
});

instance-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
};

テンプレート/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の既定の単体テストに進むと、エラー で失敗します。preloadstore 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);
});

ありとなしで試してみました(Ember-Dataがページ上にある場合、これを行う必要はないneeds: [ 'service:store' ]ことが示唆されていますが、単体テストとインスタンス初期化子の両方でインポートを試みました)。

バージョン:

Ember      : 2.4.5
Ember Data : 2.5.2
4

1 に答える 1

10

インスタンス初期化子の単体テストでは、実際のstoreサービスを取得する必要はありません。そのような場合は、モック サービスを使用することをお勧めします。instance-initializer の動作は、アプリケーションによって提供されるストアにデータを配置することです。そのストアを簡単にモックできます。

モック サービスを使用したテスト コードの例:

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());
});

2 番目のオプション

もちろん、次のように関数のパラメーターをモックすることもできますappInstanceinitialize

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());
});

しかし、私は最初のものを使用することを好みます。application によって提供されるストアにデータを配置する場合のインスタンス初期化子の動作について説明しました。しかし、2 番目のオプションでは、インスタンス初期化子がappInstance のルックアップ関数も呼び出していることも確認しているようです。このテストは、実装の詳細にさらに結びついています。

于 2016-04-28T11:07:52.323 に答える