Background: I am writing unit test for angular js controllers that use angular $ resources wrapped in services (for ease of maintenance).
Controller example:
name = 'app.controllers.UsersIndexCtrl' angular.module(name, []) .controller(name, [ '$scope' '$location' '$dialog' 'Users' 'UserRoles' ($scope, $location, $dialog, Users, UserRoles) ->
Resource Services Example:
angular.module('app.services.Users', []) .factory 'Users', ['$rootScope', '$http', '$resource', '$location' , ($rootScope, $http, $resource, $location)-> baseUrl = '/users' Users = $resource baseUrl + '/:userId', {userId: '@_id'} Users.getStatus = -> console.log 'User::getStatus()' req = $http.get baseUrl + '/status' req.success (res)-> $rootScope.globalUserAccountSettings = res unless $rootScope.$$phase then $rootScope.$apply()
Most unit test examples in angular suggest using $ httpBackend and thus make fun of the $ http service in controllers. Honestly, I doubt that this is a good practice, because if that happened, I would have to hard-code the request paths in all controller tests, and I want to isolate the behavior of the device. $ httpBackend mock is really great, but only if you use $ resource in controllers directly.
A typical single test using $ httpBackend would look like this:
it 'should be able to add a new empty user profile', -> $httpBackend.flush() l = $scope.users.length $httpBackend.expect('POST', '/users').respond _.cloneDeep mockResponseOK[0] $scope.add() $httpBackend.flush() expect($scope.users.length).toEqual l + 1
What if I created an instance of a user resource class, for example:
angular.module('app.services.Users', []) .factory 'Users', -> class Users $save:(cb)-> $remove:-> @query:-> @get:->
Angular DI mechanisms will override the old app.services.Users module with this transparent way and enable me to run checks using jasmine spies.
I am concerned about the fact that I could not find a single example that supports my idea. So the question is which one would you use and why or what am I doing wrong?