如何通过Jasmine单元测试需要父指令控制器的AngularJs指令?

时间:2015-04-07 22:14:27

标签: javascript angularjs unit-testing jasmine

我有accordionPanel父指令require的控制器的AngularJs指令accordion。我需要测试accordionPanel指令,以便在调用foldUnfold函数时查看模型是否发生更改。如何编写单元测试以查看模型是否在foldUnfold调用时更改。这是我的指令的简化版本和我到目前为止的测试是在下面:

   .directive("accordion", [

            function() {
                return {
                    templateUrl: "otherurl",
                    transclude: true,
                    replace: true,
                    scope: {

                    },
                    controller: ["$scope",function($scope) {
                        this.isOneOpenOnly = function() {
                            return $scope.oneOpenOnly;
                        }
                    }],
                    link: function(scope, elem, attrs, ctrl, linker) {
                       // some code
                    }
                }
            }
        ])
        .directive("accordionPanel", [

            function() {
                return {
                    templateUrl: "urlblah",
                    transclude: true,
                    replace: true,
                    require: "^accordion",
                    scope: {},
                    link: function(scope, elem, attrs, ctrl, linker) {
                        scope.foldUnfold = function() {
                            // some logic here then
                            scope.changeThisModel=ctrl.isOneOpenOnly();
                        }
                    }
                }
            }
        ])

到目前为止我的测试:

it('Should return unfolded as true', function() {
        var scope=$rootScope.$new(),
        element=$compile("<div accordion><div accordion-panel></div></div>")(scope);
        scope.$digest();
        scope.foldUnfold(); // this is fails as scope refers to accordion but I need to access accordionPanel
        expect(scope.changeThisModel).toBe(true);
    });

问题是我无法访问accordionPanel所在的foldUnfold范围。我认为可以通过$$childHead等方式访问它,但即使可能,它似乎也不是正确的方法。那我怎么测试呢?

0 个答案:

没有答案
相关问题