简体   繁体   English

角度业力测试指令错误:意外请求:GET

[英]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.

相关问题 Angular指令的业力测试返回意外请求 - Karma test on Angular directive returns unexpected request 使用Karma进行Angular测试中的模板,意外请求 - Templates in Angular testing with Karma, Unexpected request 测试角度指令时业力“意外请求”,即使使用 ng-html2js - Karma 'Unexpected Request' when testing angular directive, even with ng-html2js Angular / Karma-预期和观察到的请求相同时出现“意外请求”错误 - Angular/Karma - “Unexpected Request” error when expected and observed request are the same 错误:意外请求:即使在karma / jasmine单元测试中使用html2js,也可以获取嵌套指令的GET views / partials / * - Error: Unexpected request: GET views/partials/* for a nested directive even when using html2js in karma/jasmine unit test 业力获得Angular指令的范围 - Karma get scope of Angular directive 错误:意外请求:POST Karma-jasmine-angular - Error: Unexpected request: POST Karma-jasmine-angular 在Karma Angular Unit Test期间&#39;错误:意外请求&#39; - 'Error: Unexpected request' during Karma Angular Unit Test 意外的请求:茉莉花角测试中的GET - Unexpected request: GET in angular jasmine testing AngularJS服务测试(Jasmine / Karma)-错误:意外请求:GET - AngularJS Service Test (Jasmine/Karma) - Error: Unexpected request: GET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM