Unit-Testing a service in Controller with Jasmine in AngularJS -


in controller i've defined following service:

crudservice.getallgroups().$promise.then(     function (response) { $scope.groups = response; },     function (error) { //error code.. } ); 

well, want test service whether gets response or not. in test script @ first i've defined function check whether service defined @ all.

test code:

describe('ctrl: testctrl', function () {     beforeeach(module('testapp'));      var scope,         crudservice,         ctrl,         backend;      beforeeach(inject(function ($controller, $rootscope, _crudservice_, $httpbackend) {         scope = $rootscope.$new();          ctrl = $controller('testctrl', {             $scope: scope         });          crudservice = _crudservice_;          backend = $httpbackend;          }));      it('should defined service getgroups', function () {         expect(crudservice.getgroups).tobedefined();     });      //this wrong!     it('should returns successful response', function () {         backend.expectget('http://localhost:63831/api/group').respond(200, 'success');         backend.flush();     }); }); 

i don't know how response in test. i'm new in unit testing , need help.

for better comprehension here service code:

//crudservice file: ... return {          getallgroups: function () {              return resservice.group.query();          } } ...   //resservice file: return {          group: $resource(baseurl + '/api/group/:id', {            id: '@id'          }, {}) } 

do has idea?

it's incorrect in sense it's not unit test. if testing controller here, should mock crudservice , test $scope.groups has been assigned correctly.

beforeeach(function () {     module(function ($provide) {         $provide.factory('crudservice', function () {             return {                 getallgroups: function () {                     return {                         $promise: null // return actual promise here                     }                 }             }         });     }); }); 
it('should set groups', function () {     expect($scope.groups).toequal('success') }); 

and need separate spec test if crudservice calling backend correctly.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

python - RuntimeWarning: PyOS_InputHook is not available for interactive use of PyGTK -