访问Angular指令的链接部分中的函数

时间:2015-02-04 02:00:05

标签: angularjs unit-testing jasmine

以下是我要测试的指令。我知道我将如何测试它,但我很难找到如何调用/触发链接部分中的函数以便开始测试它(即。scope.accountClick)。

我确信我能够这样做:

it('should call selectProfile()', function () {
            jasmine.createSpy('selectProfile');
            scope().profileClick(account, property, profile);
            expect(scope.selectProfile).toHaveBeenCalledWith(account, property, profile);
        });

但它没有被调用。有人建议我需要使用element.isolateScope().profileClick(account, property, profile);,但这不起作用

我正在测试的指令如下所示:

angular.module('pb.webSites.directives')
        .directive('pbGoogleAnalyticsManagementList', ['$window', '$document', function ($window, $document) {

            return {
                restrict: "E",
                templateUrl: "app/webSites/directives/GoogleAnalyticsManagementList.html",
                scope: {
                    googleAnalyticsProfile: '=pbGoogleAnalyticsProfile',
                    selectProfileFn: '&pbSelectProfile'
                },
                controller: 'GoogleAnalyticsManagementController',

                link: function (scope, element, attrs) {

                    scope.accountClick = function (account) {
                        //bunch of code

                        scope.getProperties(account);
                    };

                    scope.propertyClick = function (account, property) {
                        //bunch of code

                        scope.getProfiles(account, property);
                    };

                    scope.profileClick = function (account, property, profile) {
                        //bunch of code

                        scope.selectProfile(account, property, profile);
                    };
                }
            };
        }]);

0 个答案:

没有答案