Reputation: 14591
I have a decently large number of basic services, most of which are defined exactly the same:
app.factory('Group',['$resource',function (resource) {
return resource('/api/group/:group', {group:'@id'},{});
}]);
And so on. A very few are slightly different, and either have unique properties, e.g. a User
might also have activation:
app.factory('User',['$resource',function (resource) {
return resource('/api/user/:user', {user:'@id'},{
activate: {method:'PUT', url:'/api/activate/:user'}
});
}]);
Or force some expected response, e.g. GET
might give an array:
app.factory('GroupMembers',['$resource',function (resource) {
return resource('/api/group/:group/members', {group:'@id'},{
get: {method:"get",isArray:true}
});
}]);
I am looking for a sane way to unit test these. It seems like using $httpBackend
to capture all of the requests is a bit overkill, but stubbing in $resource
might be underdone. Would I be better served with either of those approaches? Or perhaps some function suite of tests that exercises all of the get/put/post/patch/delete/query and overrides for specific cases like the added activate
for User
or special get
for GroupMembers
?
Upvotes: 1
Views: 154
Reputation: 1131
IMO you should not be testing your resources as this would be testing the framework itself. You should rather test your other services and controllers that use the resources. For such tests both approaches (mocking the resources or the $httpBackend) will do the job, so you just have to pick the more straight-forward one for you. I would vote for Sunil D.'s suggestion to mock the resource as it isolates it from the test target.
If you feel you need to verify if you declared the service property you may write very simple tests just for the resources mocking the $httpBackend, but I would not waste my time in such tests as the critical part of the code is in the framework.
Upvotes: 1