2016-06-27 38 views
0

我正在尝试编写渲染句柄组件的单元测试。当测试运行时,对象为空,并且不生成HTML。我已经使用其他组件跟随此相同的布局,并且它们正确渲染。我很困惑,为什么返回的对象为null /未定义Ember单元测试渲染句柄返回null或undefined

这里是我的余烬测试代码:

import { moduleForComponent, test } from 'ember-qunit'; 
import hbs from 'htmlbars-inline-precompile'; 

moduleForComponent('csv-upload', 'Integration | Component | csv upload', { 
    integration: true 
}); 

test('it renders', function(assert) { 
assert.expect(2); 


this.render(hbs`{{csv-upload}}`); 

assert.equal(this.$().text().trim(), ''); 

// Template block usage: 
this.render(hbs` 
    {{#csv-upload}} 
    template block text 
    {{/csv-upload}} 
`); 

assert.equal(this.$().text().trim(), ''); 
}); 

从测试的输出是:

ok 32 PhantomJS 2.1 - JSHint - integration/pods/components/csv-upload/component-test.js: should pass jshint 
not ok 33 PhantomJS 2.1 - Integration | Component | device actions: it renders 
--- 
    actual: > 
     null 

从输出另一件事:

undefined is not an object (evaluating 'this.get('selected').isAny') 

回答

1

它看起来像你的组件需要一些属性进行设置(selected)在我们的例子。由于这是未定义的,组件将无法正确呈现,这反过来使测试失败。尝试将selected属性传递给您的组件,并查看是否有帮助。

注:这是转换成答案的原始评论。

+0

谢谢!我会投票答复 – ajputnam

0

来自Jesper Haug Karsrud的意见是正确的答案!谢谢!

要纠正问题,必须在渲染前为组件设置属性。这些变化来纠正问题:

this.set('myProperty', 'fakedata'); 
this.render(hbs`{{csv-upload data=myProperty}}`); 
assert.equal(this.$().html(), '<input id="ember580" type="file" class="ember-view ember-text-field">'); 
+0

有一点要记住,'this。$()'是测试容器,而不是渲染组件 –

相关问题