Javascript ';错误:意外请求';在Karma角单元测试期间
运行Javascript ';错误:意外请求';在Karma角单元测试期间,javascript,angularjs,unit-testing,jasmine,karma-runner,Javascript,Angularjs,Unit Testing,Jasmine,Karma Runner,运行grunt karma时,当指令试图获取模板时,对其中一个指令的测试失败。我使用ng-html2js作为预处理器。下面是我的一些karma.conf.js plugins: ['karma-chrome-launcher', 'karma-jasmine', 'ng-html2js', 'karma-ng-html2js-preprocessor'], preprocessors: { 'app/scripts/directi
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);
}));
});
“严格使用”;
描述('Directive:myDirective',函数(){
//加载指令的模块
在每个模块之前(模块(‘myApp’);
每个之前(模块(“模板”);
var元素,
范围
beforeach(注入(函数($rootScope){
scope=$rootScope.$new();
}));
它('最初不应显示搜索区域'),注入(函数($compile){
元素=角度。元素(“”);
元素=$compile(元素)(范围);
范围。$digest();
expect(element.find('.myClass').hasClass('myClass')).toBe(true);
}));
});
当我运行测试时,我得到
错误:意外请求:GET/scripts/directions/myDirective/myDirective.html
似乎预处理器没有正确注入模板的javascript版本
我还尝试在beforeach(模块(“”))中使用中的模板路径代码>但这会导致以下错误:
错误:[$injector:modulerr]未能实例化模块…
我如何解决这个问题?因为我在使用Yeoman工具构建我的项目,我需要在我的karma.conf.js
文件中的ngHtml2JsPreprocessor
选项中添加一个stripPrefix
:
ngHtml2JsPreprocessor: {
moduleName: 'templates',
stripPrefix: 'app'
}
我也有同样的问题。确保您的文件完全匹配。打开Google chrome控制台,检查文件路径是否完全相同
在上面的示例中,我不得不在ngHtml2JsPreprocessor.stripPrefix中添加一个“/”字符串,结果成功了。
所以我想对于约曼,你应该使用
ngHtml2JsPreprocessor: {
moduleName: 'templates',
stripPrefix: 'app/' //add a slash
}