简体   繁体   English

Angular Karma指令测试用例未编译html

[英]Angular Karma Directive Test Case not compiling html

Below is my directive-- 以下是我的指令-

define(['jquery', 'angular'],function($, angular){

    var publishToolSummaryDirective = angular.module('sdm.publishTool.directives').directive('krnPublishToolSummary',
        function($compile){
            return {
                restrict: 'EA',
                templateUrl: "apps/sdm/pages/partials/publishToolSummary.html",
                link : function(scope, element){
                    var elem =$(element);
                    scope.previousComment = "";
                    scope.displayPublishingCommentText = false;
                    scope.displayCommentBox = function(event){
                        event.preventDefault();
                        scope.displayPublishingCommentText = true;
                    };
                    scope.displayCommentLink = function(event){
                        if(event.target.textContent === "cancel"){
                             scope.publishingCommentText = scope.previousComment;

                        }else{
                            scope.previousComment = scope.publishingCommentText;
                        }
                        scope.displayPublishingCommentText = false;
                    }
                }
            }

        });
    return publishToolSummaryDirective;
})

Below is my Unit Test-- 以下是我的单元测试-

define([
    'apps/sdm/app',
    'angular',
    'jquery',
    'angular-mocks'
], function ($, angular) {
    describe("Unit testing Summary directive", function() {
        angular.mock.module('sdm.publishTool.directives');
        var compile,scope, q,url,elm;
        beforeEach(inject(["$templateCache", "$compile","$rootScope", "$q","$httpBackend",function($templateCache, $compile, $rootScope, $q, $httpBackend) {
            q = $q;
            compile = $compile;
            scope = $rootScope.$new();
            var html='<krn-publish-tool-summary></krn-publish-tool-summary>';
            elm = compile(html)(scope);
            scope.$digest();
            console.log(elm);
        }]));

        it("should compile", function(){
            expect(elm.find('button').length).toEqual(3);
            expect(elm.className).toContain('publishTool-summary');
        });

    })
})

It is not building my templateURL mentioned in the directive. 它没有构建指令中提到的templateURL。 console o/p is 控制台o / p是

Object{0: <krn-publish-tool-summary class="ng-scope"></krn-publish-tool-summary>, length: 1}

Coverage report shows that it is not even going inside the directive 2nd line. 覆盖率报告显示它甚至没有进入指令第二行。 Any help please! 请帮忙!

The html variable is not an angular element so it is not giving desired result. html变量不是有角元素,因此无法提供所需的结果。


Try changing 尝试改变

var html='<krn-publish-tool-summary></krn-publish-tool-summary>';

to

var html=angular.element('<krn-publish-tool-summary></krn-publish-tool-summary>');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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