简体   繁体   中英

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. 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.

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

The path to the template is

app\\views\\components\\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? I have tried different configs in my karma.conf.js, see commented out lines above.

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

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: 'views/components/KonstruktStdFilterbox/KonstruktStdFilterbox.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM