Javascript karma-ng-html2js-预处理器未创建模块

Javascript karma-ng-html2js-预处理器未创建模块,javascript,angularjs,unit-testing,karma-runner,Javascript,Angularjs,Unit Testing,Karma Runner,正在尝试使用karma-ng-html2js-preprocessor。Karma发现我所有的其他东西(javascript)都很好,但是当涉及到这个html预处理器时,它似乎无法从中找到并生成模块 查看下面我的选项对象。正如您所看到的,我已经向files属性添加了一个模式,并且我正在为模块使用通用名称'foo'。在测试中,每当我试图调用module('foo'),karma就会抛出一个错误 我真的希望能够使用它,这样我就不必将模板硬编码到我的单元测试或其他古怪的解决方案中 var op

正在尝试使用karma-ng-html2js-preprocessor。Karma发现我所有的其他东西(javascript)都很好,但是当涉及到这个html预处理器时,它似乎无法从中找到并生成模块

查看下面我的选项对象。正如您所看到的,我已经向files属性添加了一个模式,并且我正在为模块使用通用名称'foo'。在测试中,每当我试图调用
module('foo'),karma就会抛出一个错误
我真的希望能够使用它,这样我就不必将模板硬编码到我的单元测试或其他古怪的解决方案中

    var options = {
        files: [].concat(
            bowerFiles,
            config.specHelpers,
            clientApp + '**/*.module.js',
            clientApp + '**/*.js',
            clientApp + '**/*.html',
            '*.html',
            temp + config.templateCache.file,
            config.serverIntegrationSpecs
            ),
        exclude: [],
        coverage: {
            dir: report + 'coverage',
            reporters: [
                // reporters not supporting the `file` property
                { type: 'html', subdir: 'report-html' },
                { type: 'lcov', subdir: 'report-lcov' },
                { type: 'text-summary' } //, subdir: '.', file: 'text-summary.txt'}
            ]
        },
        ngHtml2JsPreprocessor: {
            // strip this from the file path
            // stripPrefix: clientApp,
            moduleName: 'foo'
        },
        preprocessors: {}
    };
    options.preprocessors[clientApp + '**/*.html'] = ['ng-html2js'];
    options.preprocessors[clientApp + '**/!(*.spec)+(.js)'] = ['coverage'];
    return options;
}

我知道这不一定能回答你的问题,如果有更多的信息,我会补充这个答案,但我发现这个策略很有效

我发现在单元测试时,指令、路由或状态(如果使用ui路由器)的HTML模板不应该起作用。单元测试是关于测试组件的接口;其公开可用的方法和属性。测试与HTML文档的交互实际上是使用类似于量角器的东西进行端到端测试

考虑到这一点,以下是我如何防止Karma在测试期间尝试加载模板文件

beforeEach(function() {
    module('module.name.of.the.testable.component');

    inject(function($templateCache) {
        $templateCache.put('path/to/template/as/it/appears/in/your/component.html',
            '<div></div>');
    });
});
beforeach(函数(){
模块('module.name.of.the.testable.component');
注入(函数($templateCache){
$templateCache.put('path/to/template/as/it/executes/in/your/component.html',
'');
});
});

您的
预处理器
'*.html'
可能与
clientApp+'**.html'
中的任何内容都不匹配。尝试使用相同的模式,甚至只使用
“***.html”
?你是对的,我实际上在你发表评论的同一时间将其更改为该模式,但这似乎无法修复它。我已经更新了我的原始帖子,以显示选项对象现在的样子。您的
templateUrl
属性中的路径是否包含
clientApp
是什么?例如,假设
clientApp='myApp/src'
,你的
templateUrl
属性是否像
'myApp/src/path/to/template.html'
?clientApp=“/src/client/app/”templateUrl:“src/client/app/orders/”但我也在没有clientApp的情况下尝试过它……如果你使用真正的浏览器(不是幻影)启动Karma的话,您可以查看页面的源代码并查看包含的脚本文件。查看模板以及用于将它们添加到
$templateCache
的路径/键。它们是否与
templateUrl
中的值匹配?这很酷,除非我的模板相对较大(它不是很大,但足够大,以至于在代码中包含它会很烦人)你不想在两个不同的地方维护代码吗?@z0d14c我实际上是想用
'
作为
$templateCache
条目。在单元测试时,模板真的不重要。它只是为了阻止Karma尝试加载HTML文件。如果我想测试模板的属性,那么(就像它存在一样),呃,这不重要吗?@z0d14c像什么?你可以很容易地测试分配给作用域或控制器的属性,如果你在指令中寻找DOM操作,你可以随时监视(假设Jasmine)
元素
方法