为什么compile()仅在调用$ httpBackend.verifyNoOutstandingExpectation()时运行?

时间:2014-05-04 18:27:13

标签: angularjs angularjs-directive

我有一个指令,它接受其模板的一部分(通过templateUrl设置)并将其移动到$ rootElement(因此这部分成为它的最后一个子节点)。它的定义如下:

.directive(...

    return {
    restrict: "E",
                templateUrl: templUrl,
            controller: "MyCtrl",
            scope: {
            },

            compile: function (element, attrs) {
                return {
                    post: function postLink(scope, iElement, iAttrs, controller) {
...
                        // move modal's div to the end of $rootElement so all modals will be put one after another INSTEAD OF MAKING A HIERARCHY
                        var modalElement = $("div.modal", iElement);
                        modalElement.appendTo($rootElement);

                        // compile and link the selector's modal
                        $compile(modalElement)(scope);
                    }
                };
            }
        };
    }]);

在我的茉莉花测试中,我必须调用$ httpBackend.verifyNoOutstandingExpectation()才能使测试正常运行:

it("Element must be replaced by the template", function () {
    var linkingFn = $compile("<div ng-app><mydir></mydir></div>");
    $httpBackend.verifyNoOutstandingExpectation();  // DOES NOT WORK WITHOUT IT
    var scope = $rootScope.$new();
    var element = linkingFn(scope);

    // element - is <mydir></mydir>
    var buttonElm = $("div:first-child", element);
    expect(buttonElm).not.toBeUndefined();
    expect(buttonElm.hasClass("form-group")).toBe(true);
});

如果我注释掉verifyNoOutstandingExpectation()指令的postLink函数在我所有的期望语句之后被调用。我不明白为什么。欢迎任何想法!

0 个答案:

没有答案