【问题标题】:Component Not Rendering Correctly During Test测试期间组件未正确呈现
【发布时间】:2015-04-04 15:20:51
【问题描述】:

我正在为我的 Ember CLI 应用程序编写集成测试,但我的一个测试每次都失败。这是我目前的测试:

// tests/integration/project-test.js
test('displays "Projects" when the user views a project from Collaborate', function() {
  visit('/collaborate');

  click('.table > tbody > tr > td > a'); // it goes to /collaborate/projects/:id
  expect(0);
});

当它导航到 /collaborate/projects/:id 时,我收到错误“无法读取 null 的属性 'setLegend'”。我将其追溯到我的组件:

// app/components/foo-comp.js
import Ember from "ember";

export default Ember.Component.extend({
  fooObject: null,
  didInsertElement: function () {
    var foo = new Foo();

    this.set('fooObject', foo);
    Ember.run.once(this, 'updateChart');
  },
  updateChart: function () {
    var foo = this.get('fooObject');
    foo.setLegend(); // this is where the error comes from
  }.observes('data','fooObject')
});

请注意,如果我将 updateChart 更改为:则测试确实通过了:

updateChart: function () {
  var foo = this.get('fooObject');

  if (foo) {
    foo.setLegend();
  }
}.observes('data','fooObject')

为什么它不会在测试期间设置该变量?它在开发和生产中运行良好,所以我不确定为什么会这样。我假设这是因为didInsertElement 在测试运行时没有被触发。想法?想法?

Ember 版本:

version: 0.1.12
node: 0.10.33
npm: 2.1.8

编辑

难道Foo() 来自外部模块?也许测试无权访问它,因此无法将其添加到页面中?我对此表示怀疑,我已经尝试将其添加到.jshintrc,但我想它仍然可能与此有关。我还在运行测试时控制台记录了Foo,它确实显示在控制台中。

老实说,这可能与将其插入 DOM 有关。我在初始化其他组件时遇到了问题,所以didInsertElement 可能没有按照我的想法运行。

【问题讨论】:

    标签: ember.js ember-cli ember-qunit


    【解决方案1】:

    嗯,也许您的测试没有等待加载所有内容?

    我不确定,但你可以试试:

    test('displays "Projects" when the user views a project from Collaborate', function() {
      expect(0);
      visit('/collaborate');
      andThen(function() {
        click('.table > tbody > tr > td > a');
      });
    });
    

    【讨论】:

    • 恐怕这行不通。 Foo() 来自外部模块,有可能是这样吗?我会把它添加到我的问题中。
    猜你喜欢
    • 2020-02-19
    • 2019-08-26
    • 2018-04-25
    • 2012-05-30
    • 2021-12-07
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多