I have several problems accessing my controller using the directive I'm trying to use unit test with jasmine and karma testrunner. The directive looks like this:
directive
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' } }]);
This is the file of the test file, while I believe that everything is in order and correct (I hope):
test file file:
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 = $rootScope.$new(); element = $compile(template)(scope) ctrl = element.controller scope.$digest();
I managed to print the directive controller (I think) on the console, which returns the following ambiguous message:
LOG: function (arg1, arg2) { ... }
I cannot access any functions or properties of the directive, since they all return "undefined", what am I doing wrong?
javascript angularjs unit-testing karma-runner jasmine
nagrom97
source share