AngularJS: Unit testing controller returns "TypeError: $scope.$watch is not a function" -
i want write simple test controller
- sets variable of scope id
- calls function triggers api call id on scope
- log result
describe('the app', () => { beforeeach(angular.mock.module('mymodule')); var $controller; var eid = 123456; beforeeach(angular.mock.inject((_$controller_) => { $controller = _$controller_; })); describe('directive', () => { it('should load data api', () => { var scope = {}; var controller = $controller('mycontroller', { $scope: scope }); scope.entityid = eid; expect(scope.entityid).tobe(eid); controller.load(); // api call using scope.entityid, load data scope scope.$digest(); console.log("entities:", controller.entities); // data should loaded }); }); });
my controller uses "controller as"
syntax.
my test run karma , gives me following error:
typeerror: $scope.$watch not function | @ mycontroller.watchchanges
any hint in right direction appreciated!
you have created scope
empty object, not correct. should create new instance of $rootscope. check out code example:
describe('the app', () => { beforeeach(angular.mock.module('mymodule')); var $controller, $rootscope; var eid = 123456; beforeeach(angular.mock.inject((_$controller_, _$rootscope_) => { $controller = _$controller_; $rootscope = _$rootscope_; })); describe('directive', () => { it('should load data api', () => { var scope = $rootscope.$new(); var controller = $controller('mycontroller', { $scope: scope }); scope.entityid = eid; expect(scope.entityid).tobe(eid); controller.load(); // api call using scope.entityid, load data scope scope.$digest(); console.log("entities:", controller.entities); // data should loaded }); }); });
hope you!
Comments
Post a Comment