angularjs - Passing a dummy version of service to a controller in unit test -
i trying test controller passing dummy version of service. however, when karma runs makes call real service, specified in controller definition (line 3) instead of object try inject beforeeach(inject(function(....)
please me identify doing wrong.
//i have code following. angular.module('myapp') .controller('membersearch2ctrl', ['$scope', 'personnelservice', 'savedsearchservice', '$routeparams', '$log', function membersearch2ctrl($scope, personnelapi, savedsearches, $routeparams, $log) { // utilises personnelservice make ajax calls data server.... } ]); // real version of service makes ajax calls , returns real data angular.module('myapp') .service('personnelservice', function($http, $q) { //this.search(....) }); // dummy version of above, has same functions above returns hardcoded json angular.module('myapp') .service('personnelservicemock', function($http, $q) { // returns hardcoded json testing purpose //this.search(....) }); // heres tests describe('membersearch2ctrl', function() { var ctrl, scope, personnelapimock, savedsearchservice; beforeeach(module('myapp')); beforeeach(inject(function($rootscope, $controller, personnelservicemock, savedsearchservice, $log) { personnelapimock = personnelservicemock; // sets personnelservicemock correctly console.log(json.stringify(personnelservicemock)); // see in line console.log(json.stringify(savedsearchservice)); scope = $rootscope.$new(); ctrl = $controller('membersearch2ctrl', { $scope: scope, personnelapi: personnelservicemock, savedsearches: savedsearchservice, $routeparams: {}, $log: $log }); })); iit('upon search $scope.searchresults = personnelservice.searchpaged(...)', function() { // problem lies in next line scope.search(); // calls personnelservice.search insted of personnelservicemock.search // when have beforeeach(inject(function($rootscope, $controller, >>> personnelservicemock <<<, scope.$root.$digest(); var expected = personnelapimock.searchpaged(null, null, null); var actual = scope.searchresults; expect(actual).toequal(expected); }); });
tried passing in $injector , having injector instantiate personnelservicemock. console log says getting personnelservicemock in return. still tries making ajax call defined in personnelservice
beforeeach(inject(function($rootscope, $controller, $injector) { personnelapimock = $injector.get('personnelservicemock'); savedsearchservice = $injector.get('savedsearchservice'); log = $injector.get('$log'); console.log(json.stringify(personnelapimock)); console.log('=========================================='); console.log(json.stringify(savedsearchservice)); console.log('******************************************'); scope = $rootscope.$new(); ctrl = $controller('membersearch2ctrl', { $scope: scope, personnelapi: personnelapimock, savedsearches: savedsearchservice, $routeparams: {}, $log: log });
}));
seems specified in call ctrl = $controller('membersearch2ctrl', { ...})
being ignored , specified in controller definition (line ~3) being used.
the issue block of code:
ctrl = $controller('membersearch2ctrl', { $scope: scope, personnelapi: personnelservicemock, <!--- savedsearches: savedsearchservice, $routeparams: {}, $log: $log });
should be:
ctrl = $controller('membersearch2ctrl', { $scope: scope, personnelservice: personnelservicemock, <!--- savedsearches: savedsearchservice, $routeparams: {}, $log: $log });
Comments
Post a Comment