Angularjs: Should I use mocks when Unit Testing a REST API dependent app? -
there quite bit of ambiguities i've found when researching topic of mocks, stubs, & spies within angularjs unit testing.
i need straightforward answer how , why supposed implement unit tests proper way (if there one) on rest-dependent application.
do have use mocks when testing, or stubbing/spying suitable?
is there difference between spying , stubbing?
when mock http request, need separate mock function using particular http request?
how modularized tests need be?
take unit test example, using karma-jasmine testing framework:
describe('put request & edituser() test', function() { beforeeach(function() { $httpbackend.whenput('http://localhost:24149/users/:id'); }); it('should put request server', function() { $httpbackend.expect('get', 'http://localhost:24149/users').respond(); $scope.userslist[0] = user; $scope.selecteduser = 0; firstname = user.firstname; lastname = user.lastname; phone = '222-222-2222'; email = user.email; _id = 0001; $scope.newuseredittest = {_id, firstname, lastname, phone, email}; $httpbackend.expect('put', 'http://localhost:24149/users/1').respond(); $httpbackend.expect('get', 'templates/userprofile.html').respond(); $httpbackend.expect('get', 'templates/userslist.html').respond(); $scope.edituser($scope.newuseredittest); $httpbackend.flush(); expect($scope.userslist[0].phone).toequal('222-222-2222'); dump("put success: " + user.firstname + "'s phone number changed " + user.phone + " " + $scope.userslist[0].phone); }); });
i've been criticized test because testing put request, , edituser() function (which indirectly calls put request). wouldn't make sense test function while mocking rest requests?
if not best method, have write seperate mock service put request lies?
i assume mocks best practice when comes targeted individual units alone, have still seen examples without mocks, or spies (or stubs) , mocks , don't understand method best practice.
Comments
Post a Comment