2

我的 karma.conf.js 包括:

plugins: [
    'karma-jasmine',
    'karma-phantomjs-launcher',
    'karma-ng-html2js-preprocessor'
],
preprocessors: {
    '../../mypath/*.html': ['ng-html2js']
},
ngHtml2JsPreprocessor: {
    moduleName: 'templates'
},

(我也试过没有指定任何插件。)

我的 devDependencies 包括:

"karma-ng-html2js-preprocessor": "^0.2.0"`

我的测试包括:

beforeEach(module('templates'));

这些给出了错误:

Module 'templates' is not available!

运行 karma --log-level debug,我没有看到任何[preprocessor.html2js]条目。(我确实得到Loading plugin karma-ng-html2js-preprocessor.

我究竟做错了什么?

4

1 回答 1

6

问题是模板也必须列在下面files,并且 glob 模式preprocessors必须匹配。文档暗示了这一点。

files: [
  '../../Scripts/angular-app/directives/*.html',
  // .js files
],

preprocessors: {
  '../../Scripts/angular-app/**/*.html': ['ng-html2js']
},

注意**/*.html不匹配父目录basePath

karma start --log-level debugDEBUG [preprocessor.html2js]当一切正确时将显示条目。

我也能够删除该plugins部分。

为了获得正确的缓存 ID,我使用了:

ngHtml2JsPreprocessor: {
    // Load this module in your tests of directives that have a templateUrl.
    moduleName: 'templates',

    cacheIdFromPath: function (filepath) {
        return filepath.substring(filepath.indexOf('/Scripts/angular-app/'));
    }
},

如果模板引用了自定义过滤器,则必须加载过滤器,files并且必须在指令测试中加载过滤器的模块。

于 2016-01-15T16:41:16.337 回答