title: Unit testing promises in AngularJS with arguments
body: <p>We're unit testing our services and facing issue spying on methods with arguments of dependent services.</p>
  
  <p>I am writing unit tests for ServiceA</p>
  
  <h2>ServiceA.js</h2>
  
  <pre><code>angular.module("App").service("ServiceA", function($http, ServiceB) {
      this.detail = null; 
      this.method = function(id){
          var sevrB = new ServiceB();
          return sevrB.getId(1).then(function(response) {
              this.detail = response.data;
          }); 
      };
   });
  </code></pre>
  
  <h2>ServiceB.js (is a factory)</h2>
  
  <pre><code>(function () {
      var dependencies = [
        '../module'
      ];
      define(dependencies, function (module) {
          return module.factory('ServiceB', function ($http) {
              var ServiceB= function () {
                  this.id = null;               
              };
  
          ServiceB.prototype.getId = function(Id) {                               
              return $http.get('/test/');
          }
      }
   }());
  </code></pre>
  
  <h2>Unit test code</h2>
  
  <pre><code> describe('Testing ServiceA', function () {
   var serviceA, serviceBMock;
  
   beforeEach(function () {
       var _serviceBMock = function () {
           return {
               getId:function(id){
                   return 'test';
               }
           };
       };
  
       angular.module('ServiceAMocks', [])                   
           .value('ServiceB', _serviceBMock);
   });
  
  beforeEach(module('ServiceAMocks'));       
  
  beforeEach(inject(function (_ServiceA_, _ServiceB_) {
      serviceA=_ServiceA_;
      serviceBMock=_ServiceB_;
  });
  
  it('retrive Id', function () {  
     spyOn(serviceBMock,'getId').and.Return('test');
     serviceA.method(1);
  });
  
  }); 
  </code></pre>
  
  <p>I am spying on getId method of ServiceB from ServiceA and if i mocked ServiceB as function i am getting error below</p>
  
  <blockquote>
    <p>Error: getId() method does not exist
    at jasmineInterface.spyOn </p>
  </blockquote>
  
  <p>If I mock serviceB as object then i get error as </p>
  
  <pre><code> TypeError: object is not a function
  
   var _serviceBMock = {              
       getId:function(id){
           return 'test';
       }
   }
  </code></pre>
  
  <p>And I am not sure of testing promise in this scenario.</p>