ember.js - 无法在组件测试中找到部分内容

标签 ember.js ember-cli ember-qunit

我有一个组件测试失败,因为它找不到模板正在渲染的部分。具体错误是“断言失败:无法找到名称为“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/

相关文章:

ember.js - 如何为 Ember js 添加自定义环境?

ember.js - 添加新记录时如何立即更新UI?与 ember-cli-pagination 相关

ember.js - 应用程序和测试中的 ember 事件触发顺序不同

ember.js - Windows 上的 ember-cli 构建速度太慢

ember.js - Ember.Object.create 不再支持定义调用 _super 的方法

javascript - Ember 全局 App 变量

javascript - 找不到从`AppName/resolver Ember 测试中导入的模块 `ember-resolver`

javascript - 如何在 Controller 的 Ember.js 单元测试中设置模型数据

javascript - Ember JS 过渡到嵌套路由,其中​​所有路由都是 View 中的动态段

javascript - Ember 库导致 "Binding Style Attributes"弃用警告