【问题标题】:Karma angular testing failed to instantiate moduleKarma 角度测试无法实例化模块
【发布时间】:2016-03-02 20:28:14
【问题描述】:

我刚刚开始使用 Karma 设置一些测试。我使用 jdDom 进行了一些测试,但不喜欢它的配置方式。但是,我不确定如何正确指向 js 文件。当我收到此错误时

 Error: [$injector:modulerr] Failed to instantiate module ha.module.utility due to:
    Error: [$injector:nomod] Module 'ha.module.utility' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

我用 jsdom 启动了一个需要核心模块的文件

require('../../../src/modules/core/module.core.built.js'); 要求('../../../src/modules/utility/module.utility.built.js');

这些脚本是我的模块所在的位置。我不确定将它们放在 karma 文件中的哪个位置。 或者如果这甚至是问题。这是我下面的业力文件。我删除了 karma init 附带的 cmets,以便可以更快地阅读这篇文章。

config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: 'Scripts/',

frameworks: ['jasmine'],


// list of files / patterns to load in the browser
files: [
    'jquery /jquery libraries ',
    '../node_modules/angular/angular.js',
    '../node_modules/angular-mocks/angular-mocks.js',
    'test2/*.js',
    'tests/**/*.js'

],
exclude: [
    'tests/old/**',
    'tests/**/*.setup.js'
],


// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
    '../Templates/**/*.html' : ['ng-html2js']
},

ngHtml2JsPreprocessor: {
        // setting this option will create only a single module that contains templates
        // from all the files, so you can load them all with  angular.mock.module('foo')

        //stripPrefix: "Templates/",
        //prependPrefix: "Templates/",
        moduleName: 'templates'
    },

// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],


port: 9876,

colors: true,


logLevel: config.LOG_INFO,


autoWatch: true,



browsers: ['Chrome'],

singleRun: true,

concurrency: Infinity 

基本上我需要这些测试来找到模块​​。

【问题讨论】:

    标签: karma-runner karma-jasmine


    【解决方案1】:

    您的模块的指令、控制器和所有其他必需的文件应上传到您的“文件”列表中,如下所示:

    files: [
        '../node_modules/angular/angular.js',
        '../node_modules/angular-mocks/angular-mocks.js',
        '../../../src/modules/core/module.core.built.js',
        '../../../src/modules/utility/module.utility.built.js',
        'test2/*.js',
        'tests/**/*.js'
    
    ],
    

    【讨论】:

    • 好的,把它放在文件中是有意义的。理想情况下,我会把这些放在我的测试之前正确吗?
    • 正确。对于我的 Karma 测试,我首先拥有所有 .js 依赖项,然后是我的控制器和指令,然后是主 app.module.js,然后是测试。
    • 这个问题对于 ng2Html 预处理器也可行吗?我在启动预处理器并指向 Templates 文件夹时再次出现此错误
    猜你喜欢
    • 1970-01-01
    • 2013-10-22
    • 2014-03-07
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 2017-11-16
    • 2015-01-02
    • 2016-11-16
    相关资源
    最近更新 更多