【问题标题】:Ember-CLI tests can't include modules from 'app' directoryEmber-CLI 测试不能包含“app”目录中的模块
【发布时间】:2015-07-02 03:49:45
【问题描述】:

我正在尝试通过其中一项测试在我的应用程序中包含一个模块。甚至有可能做到这一点吗?我只能在“测试”目录中包含一个模块。 我不断收到臭名昭著的“找不到模块”错误。

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()”包括在内,但可惜没有成功。 请帮忙。

【问题讨论】:

    标签: ember.js ember-cli ecmascript-6


    【解决方案1】:

    应该没有问题。您将需要在您的 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 目录。

    【讨论】:

    • 是的,我注意到“需要”属性,我只是不明白如何使用引用的模块(“需要”中的那个)。假设我指定了一个名为 util:graph-loader 的辅助模块,它导出一个具有一些实用功能的对象。使用subject.get('util.graph-loader') 显然对我没有帮助,还有什么我可以尝试的吗?
    • 查看我修改后的答案。在意识到“utils”目录不是标准测试解析器的一部分之前,我匆忙回答。
    • 最后一行的非直观部分真的很有帮助。谢谢
    猜你喜欢
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 2014-06-14
    • 1970-01-01
    • 2021-01-19
    • 2014-11-03
    • 2014-11-26
    • 2023-03-28
    相关资源
    最近更新 更多