title: Testing $resource services in AngularJS
body: <p>I am trying to begin writing unit tests for my angular application and hit a stopping block pretty quick as I am unsure of how exactly to mock my service in a testable way.<br>
  Is there a way to mock the REST call otherwise it would seem like I need to mirror everything within my service in my tests which doesn't seem right to me, but I am rather new to test writing so maybe this is how it is supposed to be accomplished.  Any help would be greatly appreciated.</p>
  
  <p>My service is as follows:</p>
  
  <pre><code>angular.module('resources.users', ['ngResource'])
  .factory('User', function($resource) {
     var resource = $resource('/api/index.php/users/:username', {}, {
        'update': {method: 'PUT'}
     });
  
     resource.getUser = function(username, successCb) {
        return resource.query({username: username}, successCb);
     };
  
     return resource;
  });
  </code></pre>
  
  <p>My test consists thus far of:</p>
  
  <pre><code>describe('User', function() {
      var mockUserResource;
      beforeEach(module('resources.users'));
      beforeEach(function() {
          mockUserResource = sinon.stub({
              getUser: function(username) {
                  mockUserResource.query({username: username});
              },
              query: function() {}
          });
          module(function($provide) {
              $provide.value('User', mockUserResource);
          })
     });
     describe('getUser', function() {
        it('should call getUser with username', inject(function(User) {
            User.getUser('test');
            expect(mockUserResource.query.args[0][0]).toEqual({username: 'test'});
        }));
     })
  });
  </code></pre>