Directive with templateUrl - testing with ng-html2js

陌路散爱 提交于 2020-01-13 10:36:10

问题


I created a directive with inline template, tested it, everything worked fine. Now I have put the template into separated .html file and reference it via templateUrl from directive. Directive works on my page, but the tests are broken saying:

        Error: [$injector:modulerr] Failed to instantiate module source/html/par
tials/template-directive-my-directive.html due to:
        Error: [$injector:nomod] Module 'source/html/partials/template-directive
-my-directive.html' is not available! You either misspelled the module name
or forgot to load it. If registering a module ensure that you specify the depend
encies as the second argument.

I used ng-html2js preprocessor with no luck (the error above), trying different paths and prefixes in my karma config and unit test module loading. Can anybody see what is wrong (which path, or maybe something different?) ?

My structure is:

ui/Gruntfile.js // contains the karma config
ui/source/html/index.html
ui/source/html/partials/template-directive-my-directive.html
ui/source/js/my-directive.js
ui/source/tests/unit/unit-my-directive.js

Inside of Gruntfile.js:

karma : {
    unit : {
        options : {
            files : [
                'source/lib/angular/angular.js', // angular first
                'source/lib/angular/*.js', // then any other angular files...
                'source/lib/x2js/xml2json.min.js',
                'source/lib/jquery/jquery.js',
                'source/lib/ui-bootstrap/ui-bootstrap.js',
                'source/js/*.js',
                'source/tests/unit/*.js',
                'source/html/partials/*.html', // because of directive templates
            ],
            autoWatch : true,
            frameworks : ['jasmine', 'detectBrowsers'],
            plugins : [
                'karma-junit-reporter',
                'karma-jasmine',
                'karma-chrome-launcher',
                'karma-firefox-launcher',
                'karma-ie-launcher',
                'karma-safari-launcher',
                'karma-opera-launcher',
                'karma-phantomjs-launcher',
                'karma-detect-browsers'
            ],
            // generate js files from html templates to expose them during testing
            preprocessors : {
                'source/html/partials/*.html' : 'ng-html2js'
            }
            // ,
            // ngHtml2JsPreprocessor : {
                // stripPrefix : 'source/html/', // tried this, no luck
                // moduleName: 'foo' // tried this, no luck
            // }
        }
    }
}

Inside of my test (unit-my-directive.js):

// load the template
beforeEach(module('source/html/partials/template-directive-my-directive.html'));

Inside of my directive (my-directive.js):

templateUrl : 'partials/template-directive-my-directive.html', // this is ok, because actual app works

Running the test from within ui folder, where the Gruntfile.js resides


回答1:


Solution: after I spent hours, this ST answer helped me. All I needed is to register ng-html2js in karma plugins

plugins : [
                        'karma-junit-reporter',
                        'karma-jasmine',
                        'karma-chrome-launcher',
                        'karma-firefox-launcher',
                        'karma-ie-launcher',
                        'karma-safari-launcher',
                        'karma-opera-launcher',
                        'karma-phantomjs-launcher',
                        'karma-detect-browsers',
                        'karma-ng-html2js-preprocessor' //added this
                    ],


来源:https://stackoverflow.com/questions/22966697/directive-with-templateurl-testing-with-ng-html2js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!