Angular.js单元/集成测试-触发链接功能

Angular.js Unit/Integration Testing - Trigger Link Function

本文关键字:链接 功能 集成测试 js 单元 Angular      更新时间:2023-09-26

目前,我正在为一个指令编写一个测试(使用Jasmine),我怀疑链接函数没有被触发。

指令如下:

.directive('userWrapperUsername', [
    'stringEntryGenerateTemplate',
    'stringEntryGenerateLinkFn',
    // UserWrapper username column
    // Attribute: 'user-wrapper-username'
    // Attribute argument: A UserWrapper object with a 'newData' key into an
    //                     object, which contains a 'username' key holding the
    //                     UserWrapper's username
    function(stringEntryGenerateTemplate, stringEntryGenerateLinkFn) {
        return {
            template: stringEntryGenerateTemplate('username'),
            restrict: 'A',
            scope: true,
            link: stringEntryGenerateLinkFn('userWrapperUsername', 'username')
        };
    }
])

因此,它使用了工厂提供的两个功能,即stringEntryGenerateTemplatestringEntryGenerateLinkFn

stringEntryGenerateTemplate函数接收一个字符串并返回一个字符串。

stringEntryGenerateLinkFn函数在被调用时返回实际的链接函数。它主要由事件处理程序组成,所以我将把它简化为:

function stringEntryGenerateLinkFn(directiveName, key) {
    return function(scope, element, attr) {
        scope.state = {};
        scope.userWrapper = scope.$eval(attr[directiveName]);
    }
}

以下是我如何使用指令:

<div user-wrapper-username="u"></div>

这是我的测试用例:

describe('UserWrapper Table string entry', function() {
    var $scope
      , $compile;
    beforeEach(inject(function($rootScope, _$compile_) {
        $scope = $rootScope.$new();
        $compile = _$compile_;
    }));
    it('should be in stateDisplay if the value is non empty', function() {
        var userWrapper = {
                orgData: {
                    student: {
                        hasKey: true,
                        value: 'abcdef'
                    }
                },
                newData: {
                    student: {
                        hasKey: true,
                        value: 'abcdef',
                        changed: false
                    }
                }
            }
          , key = 'student'
          , elem
          , elemScope;
        $scope.userWrapper = userWrapper;
        elem = $compile('<div user-wrapper-username="userWrapper"></div>')($scope);
        elemScope = elem.scope();
        expect(elemScope.userWrapper).toBe(userWrapper);
        expect(elemScope.state).toEqual(jasmine.any(Object)); // this fails
    });
});

所以我得到一个测试失败,说elemScope.state是未定义的。回想一下,我有一个scope.state = {};语句,如果执行了链接函数,就应该执行它。我在链接函数中尝试了console.log,但它没有被执行。

那么我该如何触发链接功能呢?

谢谢!

事实证明,我必须初始化包含工厂stringEntryGenerateTemplatestringEntryGenerateLinkFn的模块,该模块与包含userWrapperUsername指令的模块相同,方法是将其添加到我的测试用例中:

beforeEach(module('userWrapper', function() {}));

其中CCD_ 11是模块的名称。

因此,测试用例变成:

describe('UserWrapper Table string entry', function() {
    var $scope
      , $compile;
    beforeEach(module('userWrapper', function() {}));
    beforeEach(inject(function($rootScope, _$compile_) {
        $scope = $rootScope.$new();
        $compile = _$compile_;
    }));
    it('should be in stateDisplay if the value is non empty', function() {
        var userWrapper = {
                orgData: {
                    student: {
                        hasKey: true,
                        value: 'abcdef'
                    }
                },
                newData: {
                    student: {
                        hasKey: true,
                        value: 'abcdef',
                        changed: false
                    }
                }
            }
          , key = 'student'
          , elem
          , elemScope;
        $scope.userWrapper = userWrapper;
        elem = $compile('<div user-wrapper-username="userWrapper"></div>')($scope);
        elemScope = elem.scope();
        expect(elemScope.userWrapper).toBe(userWrapper);
        expect(elemScope.state).toEqual(jasmine.any(Object)); // this fails
    });
});

这对我来说似乎是一个很大的疏忽。希望这将帮助任何面临类似问题的人。