javascript - 单元测试 Angular Directive(指令)

标签 javascript angularjs unit-testing

所以我正在对指令进行单元测试,但我无法编译它......

指令

home.directive('myDirective', function($compile) {
    return {
        restrict: 'E',
        scope: {
            text: '@',
            href: '@'
        },
        link: function(scope, elem, attrs) {            
            var text = '<span>Text: <a href="'+scope.href+'">'+scope.text+'</a><br />';
            text += 'HREF: '+scope.href+'</span>';
            var newElement = $compile(text)(scope);            
            elem.replaceWith(newElement);
        }
    };
})

测试

describe( 'Test home controller', function() {
    beforeEach( module( 'home' ) );
    describe("myDirective", function() {    
        var $compile;
        beforeEach(inject(function(_$compile_, _$rootScope_){
            $compile = _$compile_;
            $rootScope = _$rootScope_;
        }));
        it ("Should transform my text", function() {
            element = $compile('<my-directive text="test text" href="http://yahoo.com"></my-directive>')($rootScope);
            $rootScope.$digest();
            console.log(element.text());
        });
    });
});

我的测试中的 console.log(element.text()) 吐出一个空字符串...我可以让它读取属性...即如果我添加 Expect(element.attr('text') ).toBe("test text") 它会通过,但显然不会测试我的实际指令,也不是我想要做的。

最佳答案

尝试像这样重写你的指令:

home.directive('myDirective', function($compile) {
    return {
        restrict: 'E',
        scope: {
            text: '@',
            link: '@'
        },
        template: '<p>Text: <a ng-href="{{link}}">{{text}}</a></p>',
        replace: true
    };
});

然后你可以像这样测试:

describe( 'Test home controller', function() {
    beforeEach( module( 'home' ) );
    describe("myDirective", function() {    
        var $compile,
            $rootScope;
        beforeEach(inject(function(_$compile_, _$rootScope_){
            $compile = _$compile_;
            $rootScope = _$rootScope_;
        }));
        it ("Should transform my text", function() {
            element = $compile('<my-directive text="test text" link="http://yahoo.com"></my-directive>')($rootScope);
            $rootScope.$digest();
            console.log(element.text());
        });
    });
});

更新 - 第二种方法

此更新的方法演示了如何将链接函数与 $observe 一起使用。

尝试像这样重写你的指令:

home.directive('myDirective', function() {
    return {
        restrict: 'E',
        transclude: true,
        template: '<p>Text: <a ng-href="{{link}}">{{text}}</a></p>',
        link: function(scope, elm, attrs){
          attrs.$observe('text', function(newVal){
            scope.text = newVal + " tada";
          });
          attrs.$observe('link', function(newVal){
            scope.link = newVal + '/search?p=foo';
          });
        }
    };
});

然后你可以像这样测试:

describe( 'Test home controller', function() {
    beforeEach( module( 'home' ) );
    describe("myDirective", function() {    
        var $compile;
        beforeEach(inject(function(_$compile_, _$rootScope_){
            $compile = _$compile_;
            $rootScope = _$rootScope_;
        }));
        it ("Should transform my text", function() {
            element = $compile('<my-directive text="test text" link="http://yahoo.com"></my-directive>')($rootScope);
            $rootScope.$digest();
            expect(element.text()).toBe('Text: test text tada');
        });
    });
});

Here is a working plunker

关于javascript - 单元测试 Angular Directive(指令),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23497993/

相关文章:

JavaScript 结果

javascript - 如何在 AngularJS 中用另一个 subview 替换 subview

angularjs - UI-Grid 分页模板可编辑

Java List.contains 具有 double 和公差的对象

java - JUnit : When do I declare a TemporaryFolder object?

javascript - 为什么我无法渲染 View 的两个实例?

javascript - qTip2 的动态背景颜色

java - 在 Android 代码中模拟创建 AsyncTasks 和 Intents 的优雅方法

javascript - 为给定表格行中的每个单元格设置 CSS 类的有效方法是什么?

javascript - 为 AngularJS Web App 导入 JSON 数据