我有一个组件测试失败,因为它找不到模板正在渲染的部分。具体错误是“断言失败:无法找到名称为“components/activity-list-item-content”的部分。”
我的测试文件基本上是ember-cli默认生成的:
import {
moduleForComponent,
test
} from 'ember-qunit';
moduleForComponent('activity-list-item', 'ActivityListComponent', {
// specify the other units that are required for this test
needs: ['helper:format-date']
});
test('it renders', function() {
expect(2);
// creates the component instance
var component = this.subject();
equal(component._state, 'preRender');
// appends the component to the page
this.append();
equal(component._state, 'inDOM');
});
组件模板如下所示:
{{#if activity.momentId}}
{{#link-to 'moment' momentId class='close-dropdown'}}
{{partial 'components/activity-list-item-content'}}
{{/link-to}}
{{else}}
{{partial 'components/activity-list-item-content'}}
{{/if}}
我的应用程序工作正常,没有任何错误,所以我想知道我的测试设置中是否缺少某些内容。我还尝试将其添加到 needs
数组中并得到相同的错误:
needs: ['helper:format-date', 'template:components/-activity-list-item-content']
如何让我的测试找到部分内容?
更新
@GJK 指出部分名称应以下划线开头,而不是破折号。如果我进行更改,测试就会通过,但是我会在控制台中收到一条弃用警告,其中显示:
DEPRECATION: Modules should not contain underscores. Attempted to lookup "myapp-ember/templates/components/-activity-list-item-content" which was not found. Please rename "myapp-ember/templates/components/_activity-list-item-content" to "myapp-ember/templates/components/-activity-list-item-content" instead.
最佳答案
发现此问题:https://github.com/rwjblue/ember-qunit/issues/110
当前的解决方法是:
import resolver from '../../helpers/resolver';
moduleForComponent('some-other-thing', 'component:some-other-thing', {
needs: ['component:some-thing'],
setup: function() {
this.container.register('template:path/to/partial',
resolver.resolve('template:path/to/-partial'));
}
});
关于ember.js - 无法在组件测试中找到部分内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26385153/