javascript - How to solve 'undefind is not a function' in my case -


i trying create unit test controller.

i have like

angular.module('myapp').controller('testctrl', ['$scope', 'testfactory',     function($scope, testfactory) {         //i getting error when ran karma         //typeerror: 'undefined' not function (evaluating           //'$scope.$watch')         $scope.$watch(function(){             return testfactory.returnitem; // watch factory returned data         }, function(newval){             console.log('call here')         });     } ]} 

in factory file

angular.module('myapp').factory('testfactory', ['product','$cookies','$q',     function(product ,$cookies, $q) {         var service = {};         service.getproduct = function() {             var deferred = $q.defer();             var = this;             product.query({                 id: 123,             }, function(product) {                            that.returnitem = product;                 deferred.resolve(product);             });             return deferred.promise;         };         return service;     } ]); 

my unit test

describe('test ', function () {     beforeeach(module('myapp'));     var $controller, testfactory;      // initialize controller , mock scope     beforeeach(inject(function(_$controller_, _testfactory_){         $controller = _$controller_;         testfactory = _testfactory_;     }));      describe('initial test', function() {         it('should return data', function() {             var $scope = {};             var controlelr = $controller('testctrl', {$scope:$scope});             //not sure next….         });     }); }) 

i stuck @ error message , not sure factory test. not sure how can test getproduct method service in controller. can me it? lot!

in unit test, need create new scope object, not empty object literal. , need inject testfactory object when instantiating controller. off top of head, this:

describe('test ', function () {     beforeeach(module('myapp'));     var $controller, testfactory, scope;      // initialize controller , mock scope     beforeeach(inject(function(_$controller_, _$rootscope_, _testfactory_){         $controller = _$controller_;         $rootscope = _$rootscope_;         testfactory = _testfactory_;     }));      scope = $rootscope.$new();      describe('initial test', function() {         it('should return data', function() {             var controller = $controller('testctrl', {$scope:scope, testfactory:testfactory});             // expect here         });     }); }) 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -