title: Unit testing an asynchronous service in angularjs
body: <p>I am trying to unit test a service which has asynchronous methods but am having no luck.</p>
  
  <p>I have tried to implement with promises by using the $q support in angularjs.</p>
  
  <p>Any help would be appreciated.</p>
  
  <p><a href="http://jsfiddle.net/9pBze/37/">http://jsfiddle.net/9pBze/37/</a></p>
  
  <pre><code>angular.module('myapp', ['myservice']);
  
  angular.module('myservice', []).factory('myservice', function($q) {
    var ls = {};
  
    ls.DoIt = function() {
      var deferred = $q.defer();
  
      setTimeout(function(){
          deferred.resolve(5);
      },3000);
  
      return deferred.promise;
    }
  
    return ls;
  
  });
  
  describe('services', function () {
  
      beforeEach(module('myservice'));
  
      it("should equal 2",  inject(function(myservice) {
          myservice.DoIt().then(function(returned) {
              expect(returned).toEqual(2);
          });        
      }));
  });
  </code></pre>