【发布时间】: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