1

ディレクティブをテストするために、ディレクティブのメソッドと変数にアクセスするのに苦労しています。私は何を間違っていますか?ディレクティブとテスト ファイルを添付しました。私はそれが正しいことを知っていて、それが問題ではないことを知っているので、カルマconfを含めませんでした。

accountsearch.spec.js <-- これはテスト ケースを含むファイルです

 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()
});

これは実際のディレクティブです。コントローラーにアクセスするために非常に多くの方法を試しましたが、何が間違っているのかわかりません。

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'
    }
}]);
4

1 に答える 1