[英]Angular karma testing directive Error: Unexpected request: GET
I am setting up a unit test for angular directive with karma and I'm stuck with configuring the ngHtml2JsPreprocessor preprocessor. 我正在使用业力设置角度指令的单元测试,并且无法配置ngHtml2JsPreprocessor预处理器。 I have tried a bunch of different ways as described in various thread at Stack Owerflow of setting up the tests but am struggeling in finding a way that works for me.
我在Stack Owerflow的各种线程中尝试了多种方法来设置测试,但是在寻找一种对我有用的方法方面却很努力。
karma.conf.js karma.conf.js
files: [
'bower_components/angular/angular.js',
..
..
..
'test/mock/*.js',
'test/mock/**/*.js',
'test/spec/unit/**/*.js',
'app/views/*.html',
'app/views/**/*.html'
//'app/views/**/**/*.html'
],
preprocessors: {
//'app/views/**/**/*.html': ['ng-html2js']
'app/views/**/*.html': ['ng-html2js']
},
ngHtml2JsPreprocessor: {
// strip this from the file path
stripPrefix: 'app/',
moduleName: 'PreprocessedTemplates'
},
Test file 测试文件
beforeEach(inject(function ($compile, $rootScope, $templateCache, $httpBackend) {
scope = $rootScope;
//$templateCache.put('/app/views/components/KonstruktStdFilterbox/KonstruktStdFilterbox.html', $templateCache.get('/app/views/components/KonstruktStdFilterbox/KonstruktStdFilterbox.html'));
//$templateCache.put('../../../views/components/KonstruktStdFilterbox/KonstruktStdFilterbox.html', $templateCache.get('../../../views/components/KonstruktStdFilterbox/KonstruktStdFilterbox.html'));
ele = angular.element(
'<konstrukt-std-filterbox title="Testing generic filter Component" items="list" source="data" filter-entity="Dim1"></konstrukt-std-filterbox>'
);
mockupDataFactory = konstruktMockupData.getInstance();
//these variables are needed.
scope.data = mockupDataFactory.pivotedData;
scope.list = ["first","second"];
scope.$apply();
}));
It fails in before each with the message. 它在每条消息之前都失败了。
Error: Unexpected request: GET ../../../views/components/KonstruktStdFilterbox/KonstruktStdFilterbox.html
错误:意外请求:GET ../../../views/components/KonstruktStdFilterbox/KonstruktStdFilterbox.html
The path to the template is 模板的路径是
app\\views\\components\\KonstruktStdFilterbox\\KonstruktStdFilterbox.html
应用程序\\意见\\分量\\ KonstruktStdFilterbox \\ KonstruktStdFilterbox.html
I understand that the test cannot find the template in the templatecache, but how can I configure karma to have the PreprocessedTemplates load my template? 我知道测试无法在模板缓存中找到模板,但是如何配置业力以使PreprocessedTemplates加载模板? I have tried different configs in my karma.conf.js, see commented out lines above.
我在karma.conf.js中尝试了不同的配置,请参见上面的注释行。
It turned out that I had a path defined in my directive which was very hard to find, hence the error 原来,我在指令中定义了一个很难找到的路径,因此出现了错误
Error: Unexpected request: GET ../../../views/components/KonstruktStdFilterbox/KonstruktStdFilterbox.html
错误:意外请求:GET ../../../views/components/KonstruktStdFilterbox/KonstruktStdFilterbox.html
angular.module('app').directive('directive', function(utilities) {
return {
restrict: 'E', //element
templateUrl: '../../../views/components/KonstruktStdFilterbox/KonstruktStdFilterbox.html',
scope: true,
replace: true,
.. ..
And when I changed the templateUrl to this the test ran fine! 当我将templateUrl更改为此时,测试运行正常!
templateUrl: 'views/components/KonstruktStdFilterbox/KonstruktStdFilterbox.html
templateUrl:'views / components / KonstruktStdFilterbox / KonstruktStdFilterbox.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.