how to access controller in directive [jasmine]

女生的网名这么多〃 提交于 2019-12-24 06:45:12

问题


I am having some difficulty accessing the methods and variables on a directive in order to test it. What am I doing wrong? I have attached my directive and the test file. I did not include my karma conf because I know it is correct and that is not the problem.

accountsearch.spec.js <-- this is the file with test cases

 describe("Account search directive logic tests", function (){
  var element,$scope,scope,controller,template

  beforeEach(module("Common.accountSearch"))


  beforeEach(inject( function (_$compile_, _$rootScope_,_$controller_,$templateCache) {
    template = $templateCache.get("components/account-search/account-search.html")
    $compile = _$compile_;
    $rootScope = _$rootScope_;
    $controller = _$controller_;
    //scope = compiledElem.scope()
    scope = $rootScope.$new();
    element = $compile(template)(scope)


    scope.$digest();
  }));




  it(" sets the account and calls back.", inject(function () {
   //scope.$digest()
   expect(element.setAccount).toBeFunction()
   expect(element.hasClass("form-control")).toBeTruthy()
   console.log(scope.$$prevSibling)




   }));
  //httpBackend.flush()
});

this is the actual directive, I have tried so many ways to access its controller but I just don't know what I am doing wrong.

angular.module('Common.accountSearch',['ngRoute'])

.directive('accountSearch', [function() {
    return {
        controllerAs: 'ctrl',
        controller: function ($scope, $element, $routeParams, $http) {

            this.setAccount = function () {
                var response = { AccountId : $scope.ctrl.searchedAccount.AccountId }
                $scope.callback(response)
            }

            this.getAccounts = function(searchText){
                return $http.get('/api/CRMAccounts', {
                    params: {
                        retrievalLimit: 10,
                        search: searchText
                    }
                }).then(function(response){
                    return response.data;
                });

            }

        },
        scope : {
            config : '=',
            values : '=',
            callback : '='
        },
        templateUrl : '/common/components/account-search/account-search.html',
        restrict : 'EAC'
    }
}]);

回答1:


You need to get the controller for the directive, which holds the methods you are trying to call.

Your element variable only holds the DOM-component.

beforeEach(inject( function (_$compile_, _$rootScope_,_$controller_,$templateCache) {
    template = $templateCache.get("components/account-search/account-search.html")
    $compile = _$compile_;
    $rootScope = _$rootScope_;
    $controller = _$controller_;
    scope = $rootScope.$new();
    element = $compile(template)(scope)
    scope.$digest();
    ctrl = element.controller('accountSearch');
  }));

And in your tests:

expect(ctrl.setAccount).toBeFunction()


来源:https://stackoverflow.com/questions/39914201/how-to-access-controller-in-directive-jasmine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!