title: Angular unit test factory that uses http request
body: <p>I am working on an angular js app with karma/Jasmine testing framework, I need to test a factory that returns a http promise but it always return undefined</p>
  
  <p>here is my factory</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>angular.module('GithubUsers').factory('Users',['$http','$q',function($http,$q){
    return{
      getAllUsers:function(){
        var defered= $q.defer();
        $http({
          url:'https://api.github.com/users',
          method:'GET'
        }).then(function(users){
          defered.resolve(users.data);
        },function(err){
          defered.reject(err);
        })
        return defered.promise;
      }
    }
  }])</code></pre>
  </div>
  </div>
  </p>
  
  <p>here is my tests  </p>
  
  <p><strong>Update</strong> thanks to your answers I modified my code to the following but no I got this error </p>
  
  <blockquote>
    <p>Possibly unhandled rejection: {"status":0,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"<a href="https://api.github.com/users?since=1" rel="nofollow noreferrer">https://api.github.com/users?since=1</a>","headers":{"Accept":"application/json, text/plain, <em>/</em>"},"cached":false},"statusText":""} thrown</p>
  </blockquote>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>describe('Test Users Factory',function(){
    var $controller,
    Users,
    $rootScope,
    $httpBackend,
    $q;
    beforeEach(module('GithubUsers'));
    beforeEach(inject(function(_$controller_,_Users_,_$rootScope_,_$httpBackend_,_$q_){
      $controller = _$controller_;
      Users = _Users_;
      $rootScope= _$rootScope_;
      $httpBackend=_$httpBackend_;
      
     
    }))
    
    it('should get users',function(){
     
      var result;
  
      $httpBackend.whenGET('https://api.github.com/users?since=1').respond(function(){
         return {data:[{id:2}],status:200};
      })
      Users.getAllUsers().then(function(res){
        result = res;
      });
      $httpBackend.flush();
      $rootScope.$digest()
      expect(result).toBeTruthy();
    })
  })</code></pre>
  </div>
  </div>
  </p>
  
  <p>Thanks in advance!</p>