angularjs - ngHtml2JsPreprocessor 不加载包含模板的模块,已使用 karma 测试

标签 angularjs karma-runner ng-html2js

使用 karma ngHtml2JsPreprocessor 测试我的 angular 应用程序,我已经阅读了数十页关于如何配置 karma 以生成 js 模板而不是 Angular 指令加载的 html 模板的页面,我坚持消息:

Module 'js_templates' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
    http://errors.angularjs.org/1.4.6/$injector/nomod?p0=js_templates

我的文件夹结构是这样的:

/System/Root/Path/      
    web/
        js/ 
            app/
                test/
                    specs/
                        UserModule/
                            UserDirectives.spec.js
                modules/    
                    UserModule/ 
                        UserDirectives.js  <=== Where the directive is defined
        Templates
            login.tpl.html

我的指令是这样定义的:

directives.directive("loginForm", [function(){
return{
    restriction: "E",
    templateUrl: assetsRootPath+'/Templates/login.tpl.html' //assetsRootPath is defined to '' for karma
}]);

Karma 配置如下:

module.exports = function(config) {
  config.set({

    basePath: './web/js/',
    frameworks: ['jasmine', 'requirejs'],
    preprocessors: {
      '../Templates/**/*.html': ['ng-html2js']
    },
    ngHtml2JsPreprocessor: {
      stripPrefix: '.*web/',
      prependPrefix: '/',
      moduleName: 'js_templates',
      enableRequireJs: false
    },
    plugins : [
      'karma-chrome-launcher',
      'karma-jasmine',
      'karma-requirejs',
      'karma-ng-html2js-preprocessor'
    ],

    // list of files / patterns to load in the browser
    files: [
      { pattern: '../Templates/**/*.html', included: false },
      //....Other needed files
    ]

测试指令的规范:

define(['angular', 'ngmocks', 'app/modules/UserModule/UserDirectives'], function() {
    describe("Test User Directive > ", function () {
        var $compiler, compiledElement;
        beforeEach(module('User.Directives'));
        beforeEach(module('js_templates'));

        beforeEach(inject(function (_$rootScope_, _$compile_) {
            $rootScope = _$rootScope_;
            $compile = _$compile_;
            scope = $rootScope.$new();
            compiledElement = $compile("<login-form></login-form>")(scope);
            scope.$digest();
        }));

        it('Tries to test', function(){
            expect(1).toEqual(1);
        });
    });
});

当我启动测试时出现以下错误:

Module 'js_templates' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
    http://errors.angularjs.org/1.4.6/$injector/nomod?p0=js_templates

我尝试了很多配置但没有任何效果,我还尝试通过添加一些 log.debug 来调试 ngHtml2JsPreprocessor & 我看到模块“js_templates”已正确创建,问题是为什么我无法加载它对于规范。可能有用的信息(不确定它是否改变了什么):我正在使用 requirejs。 感谢您的帮助

最佳答案

好吧,由于没有任何配置起作用,我选择使用不同的方法,下面是我最终为使其起作用所做的事情:

1 - 删除所有预处理器 2 - 安装简单的 html2js 节点插件:npm install karma-html2js-preprocessor --save-dev 3 - 在我的 karma.conf 文件中:

pattern: { files: '../Templates/**/*.html', included : true } //Note the included property to true
preprocessors: {
  '../Templates/**/*.html': ['html2js']
},

plugins : [
  'karma-chrome-launcher',
  'karma-jasmine',
  'karma-requirejs',
  'karma-html2js-preprocessor'
],

在我的 test-main.js 文件中,它是 require-js 的入口点,我这样做了:

require([
        'angular',
        ...
    ], function(angular, app) {
        var $html = angular.element(document.getElementsByTagName('html')[0]);
        angular.element().ready(function() {
            // bootstrap the app manually
            angular.bootstrap(document, ['app']);

            //Making all templates to be availabe through js_template module
            angular.module('js_templates', []).run(function($templateCache){
                angular.forEach(window.__html__, function(content, filename){
                    var modifiedFilename = filename;
                    /*****************Modify this to fit your app**********************/
                    var breakPos = modifiedFilename.indexOf('/Templates');
                    modifiedFilename = filename.substr(breakPos, modifiedFilename.length - breakPos);
                    /*****************/Modify this to fit your app*********************/
                    $templateCache.put(modifiedFilename, content);
                });
            });
        });
    }
);

不是很锋利但也不是那么糟糕,无论如何它确实有效而且这正是我想要的。当解决方案可以自然地与 ngHtml2JsPreprocessor 一起工作时,我将使用它。

关于angularjs - ngHtml2JsPreprocessor 不加载包含模板的模块,已使用 karma 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34103200/

相关文章:

javascript - 如何在基于 angular-fullstack 的应用程序中包含库文件 (css/js)

javascript - 为什么 Angular 在正确更新模型时不通过向 ng-options 中的 <option> 标记添加 'selected' 属性来更新 DOM?

gruntjs - 试图在 gruntfile 配置中排除文件,但文件路径前缀破坏了表达式

mocha.js - 如何在 mocha 记者中获取测试的文件名

angularjs - Karma 0.12.9,单元测试,karma-ng-html2js-预处理器

javascript - angularjs,需要帮助理解 $compile 函数

javascript - AngularJS 400错误请求不给出任何响应

karma-runner - 使用 karma 和 webpack 运行测试

javascript - isolateScope() 给出未定义的 - 使用 Karma 的单元测试指令

angularjs - Karma 抛出错误 : Can not load "ng-html2js", 它未注册