【问题标题】:Compile directive failing in AngularJS testing - karma jasmine编译指令在 AngularJS 测试中失败 - 业力茉莉
【发布时间】:2017-05-26 21:07:39
【问题描述】:

我正在使用 Karma 和 Jasmine 框架来测试我的 AngularJS v1 应用程序。我已经设置了 ng-html2js 插件来将部分转换为模块。我可以使用$templateCache 访问部分内容。

当我尝试编译指令时,我什么也没得到。 url 的模板使用templateUrl 指定。

我的项目结构是这样的。

project
|--index.php
|--karma.conf.js
|--assets
   |--app
      |--controllers
      |--directives
         |--my-directive.js
      |--partials
         |--my_directive.html
      |--tests
         |--directive-test.js

karma.conf.js

...
preprocessors: {
        "assets/app/partials/**/*.html": ['ng-html2js']
},
files: [
    ...
    'assets/app/controllers/**/*.js',
    'assets/app/directives/**/*.js',
    'assets/app/partials/**/*.html',
    'assets/app/tests/**/*.js',
],
ngHtml2JsPreprocessor: {
        moduleName: 'my.partials'
},
...

directive-test.js

describe('Test My Directive', function () {

    var $compile, $rootScope;

    beforeEach(function () {
        module('myapp');
        module('my.partials');

        inject(function (_$compile_, _$rootScope_, _$templateCache_) {
            $compile = _$compile_;
            $rootScope = _$rootScope_;
            $templateCache = _$templateCache_;

            // This prints the template to the console
            console.log($templateCache.get('assets/app/partials/my_directive.html'));

        });
    });

    it('check if directive compiles', function () {

        var scope = $rootScope.$new();
        var template = $compile('<my-directive></my-directive>')(scope);
        scope.$digest();

        var templateAsHtml = template.html();

        // This doesn't print the template to the console
        console.log('templateAsHtml', templateAsHtml);

        expect(templateAsHtml).toContain("Lorem ipsum");

    });
});

my-directive.js

myapp.directive('myDirective', function() {
    return {
        replace:true,
        restrict:'E',
        templateUrl: 'assets/app/partials/my_directive.html',
        link: function (scope, element, attrs) {
            console.log('my directive');
        }
    }
});

assets/app/partials/directives/my_directive.html

<div><h2>Test Directive</h2><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam et enim non sem bibendum maximus. Nunc at urna sit amet.</p></div>

【问题讨论】:

    标签: angularjs jasmine karma-jasmine angular-directive ng-html2js


    【解决方案1】:

    如果我没记错的话,templateUrl总是是一个异步请求,即使指定的 url 已经在 $templateCache 中。在你的指令定义中试试这个:

    template: function($templateCache) {
        return $templateCache.get('assets/app/partials/my_directive.html');
    }
    

    【讨论】:

    • 我尝试了以下template: $templateCache.get('assets/app/partials/my_directive.html') 。在我的测试脚本中,它能够获取模板并编译它,因为 ng-html2js 已经将它加载到 $templateCache 中。但是当我在网页上使用该指令时,它不会呈现。
    • 等等,你不是在非测试环境中运行ng-html2js吗?如果可行的话,我强烈建议预先缓存所有模板。我在我的开发环境中使用ng-templates,它就像一个魅力。
    • 没有。我没有在我的测试环境之外使用 ng-html2js。您是否有链接来演示如何在测试环境之外使用它?
    【解决方案2】:

    我以这种方式解决了它 - 感谢@Aaron Pool 的提示。在指令中使用 templateUrl 会发出异步请求/AJAX 调用以获取模板。可以使用 $httpBackend 拦截 $http AJAX 调用。捕获 AJAX 调用并使用来自$templateCache 的模板进行响应。下面是修改后的代码,假设 $httpBackend 被注入到 beforeEach() 块中。

    it('check if directive compiles', function () {
    
        var scope = $rootScope.$new();
    
        // Intercept the AJAX to fetch template
        $httpBackend.whenGET('assets/app/partials/directives/my_directive.html')
        .respond($templateCache.get('assets/app/partials/directives/my_directive.html'));
    
        var template = $compile('<my-directive></my-directive>')(scope);
    
        $httpBackend.flush();
    
        scope.$digest();
    
        var templateAsHtml = template.html();
    
        // Now it prints the template to the console
        console.log('templateAsHtml', templateAsHtml);
    
        expect(templateAsHtml).toContain("Lorem ipsum");
    
    });
    

    【讨论】:

    • 不错!真聪明!与此错误完全无关,如果您有时间,我仍然肯定会研究 ng-templates。这是一个 grunt/gulp 插件,可以自动将所有模板添加到缓存中。在预测嵌套指令的链接顺序时,这可以为您省去很多麻烦。 :)
    • 谢谢!会调查的。
    猜你喜欢
    • 2023-04-06
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 2015-04-01
    • 2015-09-19
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    相关资源
    最近更新 更多