javascript - $scope.$apply undefined angularJS unit testing jasmine -
i'm testing controller containing
$document.on('click', $scope.$apply.bind($scope, $scope.deactivate));
when test controller using jasmine & karma
'use strict'; describe('controllers: arrayctrl', function() { var scope; beforeeach(module('ironridge')); beforeeach(inject(function($controller,$rootscope) { scope = $rootscope.$new(); $controller('arrayctrl', { $scope: scope }); })); it('test section ', inject(function($controller) { expect(scope.pannels.length).tobe(0); })); });
i following error :
phantomjs 1.9.8 (linux 0.0.0) controllers: arrayctrl should located in quote section failed typeerror: 'undefined' not function (evaluating '$scope.$apply.bind($scope, $scope.deactivate)') undefined @ /home/hpro/ironridge/src/app/components/array/array.controller.js:183 @ invoke (/home/hpro/ironridge/bower_components/angular/angular.js:4219) @ instantiate (/home/hpro/ironridge/bower_components/angular/angular.js:4227) @ /home/hpro/ironridge/bower_components/angular/angular.js:8533 @ /home/hpro/ironridge/bower_components/angular-mocks/angular-mocks.js:1878 @ /home/hpro/ironridge/src/app/components/array/array.controller.spec.js:11 @ invoke (/home/hpro/ironridge/bower_components/angular/angular.js:4219) @ workfn (/home/hpro/ironridge/bower_components/angular-mocks/angular-mocks.js:2437) typeerror: 'undefined' not object (evaluating 'scope.pannels.length') undefined @ /home/hpro/ironridge/src/app/components/array/array.controller.spec.js:17 @ invoke (/home/hpro/ironridge/bower_components/angular/angular.js:4219) @ workfn (/home/hpro/ironridge/bower_components/angular-mocks/angular-mocks.js:2437)
help please
$document.on('click', $scope.$apply.bind($scope, $scope.deactivate));
i don't think above code works. guess trying apply scope. can do
$(document).on('click', function(){ if(!$scope.$$phase){ $scope.$apply(); } $scope.deactivate(); });
coming test case, bind function in angularjs used as
angular.bind(self, fn, args)
according documentation. in jquery, bind used attach handler event elements. in case $scope.$apply.bind not work. reason test case throwing error.
Comments
Post a Comment