Ember-CLI 测试不能包含来自 'app' 目录的模块

Ember-CLI tests can't include modules from 'app' directory

我正在尝试在我的应用程序中包含一个测试中的模块。甚至可以这样做吗?我只能在 'tests' 目录中包含一个模块。 我一直收到臭名昭著的 "Could not find module" 错误。

http://localhost:4200/assets/test-support.js:5578:16: Could not find module d3graph/app/controllers/index imported from d3graph/tests/unit/utils/graph-helper-test

这是我的测试代码:

import { moduleFor, test }  from 'ember-qunit';
import Ember from 'ember';
import helper from '../../../app/anything/anywhere';  // <- THIS LINE FAILS

moduleFor('util:graph-helper', 'Graph Helper', {
  beforeEach: () => initialize()
});

function initialize() { /* something */ };

test('test desc', function(assert) {
  var testObj = this.subject();

  // test logic follows
});

我确实尝试了对模块路径的各种修改,包括从根目录的绝对路径,我什至尝试通过 'require()' 包含,但可惜没有成功。 请帮忙。

应该不是问题。您需要在 moduleFor 调用中加入 needs 行:

import { moduleFor, test }  from 'ember-qunit';
import Ember from 'ember';

moduleFor('util:graph-helper', 'Graph Helper', {
    needs: ['controller:index'],

   beforeEach: () => initialize()
});

function initialize() { /* something */ };

test('test desc', function(assert) {
    var testObj = this.subject();

    // test logic follows
});

有关 needs 的更多详细信息,请参阅 http://guides.emberjs.com/v1.10.0/testing/testing-controllers/#toc_testing-controller-needs

编辑

忽略上面的信息...那是 Ember 解析标准方式的模块。要将模块包含在 Ember 路径之外,一个简单的 ES6 导入就足够了(这个例子演示了为 controller:index 单元测试引入 some-util):

import { moduleFor, test }  from 'ember-qunit';
import Ember from 'ember';
import SomeUsefulUtil from '<application-name>/utils/some-useful-util';

moduleFor('controller:index', 'Graph Helper', {
   beforeEach: () => initialize()
});

function initialize() { /* something */ };

test('test desc', function(assert) {
    var testObj = this.subject();

    // Create utility class instance
    var someUsefulUtilInstance = new SomeUsefulUtil();

    // test logic follows
});

其中可能不直观的部分是您必须在导入前加上您的应用程序名称而不是标准 app 目录。