1

我是 ember 社区的新手,我创建了一个组件,它基于给定的 DS.Model 对象生成其内容:

// templates/test.hbs
{{my-component model=model.model modelName=model.modelName}}

// routes/test.js
import MyModel from 'app/models/my-model

export default Ember.Route.extend({
  model() {
    model: MyModel,
    modelName: 'my-model'
  }
});

// components/my-component.js
export default Ember.Component.extend({
  modelAttributes: Ember.computed(function() {
    let model = this.get('model');
    let attributes = [];
 
    // retrieves options if exists
    let labels = this.get('labels');
    let requires = this.get('requires');

    // Populating modelAttributes
    model.eachAttribute((attr, meta) => {
      let attributesInfo = {key: attr, type: meta.type, data: ''};
      // do some stuff
      attributes.push(attributesInfo);
    });
    return attributes;
  })
});

它工作得很好,但我很坚持测试这个。确实我试过:

// tests/acceptances/components/my-component
moduleForComponent('my-component', 'Integration | Component | my component', {
  integration: true
});

const DummyModel = DS.Model.extend({
  string: DS.attr('string'),
  email: DS.attr('email')
});

test('it renders', function(assert) {
  server.createList('dummy', 5);

  this.set('modelDescription', DummyModel);
  this.set('modelName', 'dummy');

  this.render(hbs`{{easy-crud modelName=modelName model=modelDescription}}`);

  // asserting stuff
  
 });

并使用海市蜃楼来伪造虚拟物体。但它继续失败并出现以下错误:

Died on test #1 at Module.callback (http://localhost:7357/assets/tests.js:698:24)
    at Module.exports (http://localhost:7357/assets/vendor.js:123:32)
    at requireModule (http://localhost:7357/assets/vendor.js:38:18)
    at TestLoader.require (http://localhost:7357/assets/test-support.js:6979:7)
    at TestLoader.loadModules (http://localhost:7357/assets/test-support.js:6971:14)
    at Function.TestLoader.load (http://localhost:7357/assets/test-support.js:7001:22)
    at http://localhost:7357/assets/test-support.js:6885:18: Unexpected token u in JSON at position 0

有人有想法吗?还是我做错了什么?

谢谢

编辑:添加用于代码理解的 ember 函数

4

0 回答 0