mardi 19 avril 2016

Instance initializer unit test fails with "store is undefined"

After generating an example application:

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

With the following files:

models/test-data.js

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  value: DS.attr( 'number' )
});

routes/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
};

templates/index.hbs

<ul>

  <li>: </li>

</ul>

adapters/application.js

import RESTAdapter from 'ember-data/adapters/rest';

export default RESTAdapter.extend({});

ember serve runs the application and displays the preload data but going to /tests the default unit test for the preload instance initializer fails with the error store is undefined.

Full Error Message:

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

How do I initialize the application's store so that it can be used in the unit test?




Aucun commentaire:

Enregistrer un commentaire