Ember.js 实例初始值设定项单元测试失败,返回“0”;“存储未定义”;

Ember.js 实例初始值设定项单元测试失败,返回“0”;“存储未定义”;,ember.js,ember-data,ember-testing,Ember.js,Ember Data,Ember Testing,生成示例应用程序后: 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: D

生成示例应用程序后:

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' )
});
import Ember from 'ember';

export default Ember.Route.extend({
  model(){
    return this.store.peekAll( 'test-data' );
  }
});
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
};
<ul>
{{#each model as |td|}}
  <li>{{td.name}}: {{td.value}}</li>
{{/each}}
</ul>
import RESTAdapter from 'ember-data/adapters/rest';

export default RESTAdapter.extend({});
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);
});
routes/index.js

import DS from 'ember-data';

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

export default Ember.Route.extend({
  model(){
    return this.store.peekAll( 'test-data' );
  }
});
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
};
<ul>
{{#each model as |td|}}
  <li>{{td.name}}: {{td.value}}</li>
{{/each}}
</ul>
import RESTAdapter from 'ember-data/adapters/rest';

export default RESTAdapter.extend({});
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);
});
实例初始值设定项/preload.js

import DS from 'ember-data';

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

export default Ember.Route.extend({
  model(){
    return this.store.peekAll( 'test-data' );
  }
});
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
};
<ul>
{{#each model as |td|}}
  <li>{{td.name}}: {{td.value}}</li>
{{/each}}
</ul>
import RESTAdapter from 'ember-data/adapters/rest';

export default RESTAdapter.extend({});
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);
});
模板/索引.hbs

import DS from 'ember-data';

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

export default Ember.Route.extend({
  model(){
    return this.store.peekAll( 'test-data' );
  }
});
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
};
<ul>
{{#each model as |td|}}
  <li>{{td.name}}: {{td.value}}</li>
{{/each}}
</ul>
import RESTAdapter from 'ember-data/adapters/rest';

export default RESTAdapter.extend({});
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-serve
运行应用程序并显示预加载数据,但转到
/tests
实例初始值设定项的默认单元测试失败,错误为
存储未定义

完整错误消息

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
Ember      : 2.4.5
Ember Data : 2.5.2
使用
needs:['service:store']
和不使用它进行了尝试(尽管它建议您在页面上是否有余烬数据-我还尝试在单元测试和实例初始化器中导入)

版本

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
Ember      : 2.4.5
Ember Data : 2.5.2

在实例初始值设定项的单元测试中,您不需要获得真正的
store
服务。在这种情况下,您更喜欢使用模拟服务。实例初始值设定项的行为是将一些数据放入应用程序提供的存储中。你可以轻松地嘲笑那家商店

模拟服务的测试代码示例:

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());
});
第二选项

当然,您也可以模拟
initialize
函数的
appInstance
参数,如下所示:

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

但我更喜欢用第一个。我们声明,您的实例初始值设定项会将一些数据放入应用程序提供的存储中。但在第二个选项中,我们似乎也在检查您的实例初始值设定项是否也在调用appInstance的查找函数。此测试更符合您的实现细节。

您是否通过
needs
在单元测试中将
service:store
定义为依赖项进行了测试?@jelhan是的,使用
needs:['service:store']
和(as)进行了尝试尝试导入
ember数据
,并一直在尝试解决如何在新实例上创建存储,但没有成功。