当运行grunt karma时,当它试图获取模板时,对其中一个指令的测试失败。我使用ng-html2js作为预处理程序。这是我的一些karma.conf.js
plugins: ['karma-chrome-launcher',
'karma-jasmine',
'ng-html2js',
'karma-ng-html2js-preprocessor'],
preprocessors: {
'app/scripts/directives/**/*.html': 'ng-html2js'
},
ngHtml2JsPreprocessor: {
moduleName: 'templates'
}在我的测试中,我有以下几点:
'use strict';
describe('Directive: myDirective', function () {
// load the directive's module
beforeEach(module('myApp'));
beforeEach(module('templates'));
var element,
scope;
beforeEach(inject(function ($rootScope) {
scope = $rootScope.$new();
}));
it('should not show search area initially', inject(function ($compile) {
element = angular.element('<navbar></navbar>');
element = $compile(element)(scope);
scope.$digest();
expect(element.find('.myClass').hasClass('myClass')).toBe(true);
}));
});当我做测试时,我得到
Error: Unexpected request: GET /scripts/directives/myDirective/myDirective.html
似乎预处理器没有正确地注入模板的javascript版本。
我还尝试使用beforeEach(module(''));中的模板路径,但这会导致一个错误,即:
Error: [$injector:modulerr] Failed to instantiate module...
,我怎样才能解决这个问题?
发布于 2014-07-31 15:02:18
我也遇到了同样的问题。确保您有准确的文件匹配。打开Google控制台并检查文件路径是否完全相同。

在上面的示例中,我不得不在ngHtml2JsPreprocessor.stripPrefix中添加一个"/“字符串,它起了作用。所以我想对于约曼,你应该用
ngHtml2JsPreprocessor: {
moduleName: 'templates',
stripPrefix: 'app/' //add a slash
}发布于 2014-05-02 14:37:11
由于我使用约曼工具为我的项目搭建脚手架,我需要在我的stripPrefix文件中的ngHtml2JsPreprocessor选项中添加一个karma.conf.js:
ngHtml2JsPreprocessor: {
moduleName: 'templates',
stripPrefix: 'app'
}https://stackoverflow.com/questions/23429126
复制相似问题