微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

angularjs – 使用templateUrl指令 – 使用ng-html2js进行测试

我用内联模板创建了一个指令,对其进行了测试,一切正常.现在我将模板放入单独的.html文件中,并通过指令中的templateUrl引用它.指令适用于我的页面,但测试被打破说:

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.

我使用ng-html2js preprocessor没有运气(上面的错误),在我的karma配置和单元测试模块加载中尝试不同的路径和前缀.任何人都可以看到什么是错的(哪条路径,或者可能是不同的东西?)?

我的结构是:

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

在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
            // }
        }
    }
}

在我的测试中(unit-my-directive.js):

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

在我的指令(my-directive.js)中:

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

从uru文件夹中运行测试,Gruntfile.js所在的位置

解决方法

解:
在我花了几个小时之后,这个 ST answer我有所帮助.我只需要在业力插件注册ng-html2js

plugins : [
                        'karma-junit-reporter','karma-detect-browsers','karma-ng-html2js-preprocessor' //added this
                    ],

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐