javascript - 如何测试仅用作模板的指令

标签 javascript angularjs angularjs-directive jasmine karma-jasmine

我正在使用 Jasmine 来测试我的 Angular 组件。我能够很好地测试功能、服务和 Controller ,但是,我如何测试仅包含模板的指令,例如:

app.directive('myDirective', function () {
    return {
        restrict: 'E',
        replace: true,
        transclude: true,
        template:   '<header>' +
                        '<p>' +
                            '<a href="http://someurl1.com/home" target="_blank">Home</a>' +
                            '&nbsp;&nbsp;|&nbsp;&nbsp;' +
                            '<a href="http://someurl2.com/cookies" target="_blank">Cookies</a>' +
                            '&nbsp;&nbsp;|&nbsp;&nbsp;' +
                            '<a href="http://someurl3.com/Terms" target="_blank">Terms</a>' +
                        '</p>' +
                        '<p>Text text text text text text text text text text text text text text text text text</p>' +
                    '</header>'        
    };
});

我尝试了一些类似的方法:

 describe('Directive: header', function () {
     beforeEach(module('headerDirective'));

     var element, scope;

     beforeEach(inject(function ($rootScope, $compile) {
         element = angular.element('<header>' +
             '<p>' +
                 '<a href="http://someurl1.com/home" target="_blank">Home</a>' +
                 '&nbsp;&nbsp;|&nbsp;&nbsp;' +
                 '<a href="http://someurl2.com/cookies" target="_blank">Cookies</a>' +
                 '&nbsp;&nbsp;|&nbsp;&nbsp;' +
                 '<a href="http://someurl3.com/Terms" target="_blank">Terms</a>' +
                 '</p>' +
                 '<p>Text text text text text text text text text text text text text text text text text</p>' +
             '</header>');

         scope = $rootScope;

         $compile(element)(scope);
         scope.$digest();
     }));

     it("should have the correct amount of <a>", function () {
         var list = element.find('a');
         expect(list.length).toBe(a);
     });
 });

最佳答案

您应该注入(inject)加载 Angular 应用程序模块,而不是指令。

describe('myDirective: ', function () {
    var service,
    scope,
    compile,
    element;

    //load the angular app module
    beforeEach(module('MyApp'));

    beforeEach(inject(function ($rootScope, $compile) {
        scope = $rootScope.$new();
        element = angular.element('<my-directive></my-directive>');
        $compile(element)(scope);
        scope.$digest();
    }));

    it('should have 3 anchor tags', function () {
        expect(element[0].querySelectorAll('a').length).toEqual(3);
    });

    it('should have a Home link', function () {
        expect(element[0].querySelectorAll('a')[0].textContent).toEqual('Home');
    });

    // add more. Test for P tags.

});

JSFFIDDLE

关于javascript - 如何测试仅用作模板的指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29140698/

相关文章:

javascript - Node.js/ express : download file via ajax

javascript - 删除表单 JS 中的元素

javascript - 为什么行点击事件在 Angular 上不能正常工作?

angularjs - net::ERR_INVALID_URL 当从 $http.get 结果设置 <img> src 时

javascript - Angular 1.5 组件不传递模态数据

javascript - 我需要 $http POST 状态代码 400,但得到 200

javascript - 如何在页面加载时自动运行功能?

javascript - AngularJS 中带有成员变量的子类工厂

javascript - 为什么这有效?清除用作表单的 javascript 对象

javascript - 在 ui-router 配置文件中使用 Angular-translate 的 $translate.use()