title: How does one stub promise with sinon?
body: <p>I have a data service with following function</p>
  
  <pre><code>function getInsureds(searchCriteria) {
  
      var deferred = $q.defer();
  
      insuredsSearch.get(searchCriteria,
          function (insureds) {
              deferred.resolve(insureds);
          },
          function (response) {
              deferred.reject(response);
          });
  
      return deferred.promise;
  }
  </code></pre>
  
  <p>I want to test following function:</p>
  
  <pre><code>function search ()
  {
    dataService
        .getInsureds(vm.searchCriteria)
        .then(function (response) {
             vm.searchCompleted = true;
  
              if (response.insureds.length &gt; 100) {
                vm.searchResults = response.insureds.slice(0, 99);
              } else {
                  vm.searchResults = response.insureds;
             }
         });
  }
  </code></pre>
  
  <p>How would I stub the promise so that when I call getInsureds it would resolve the promise and return me the results immediately. I started like this (jasmine test), but I am stuck, as I don't know how to resolve the promise and pass in arguments needed.</p>
  
  <pre><code>it("search returns over 100 results searchResults should contain only 100 records ", function () {
  
      var results103 = new Array();
  
      for (var i = 0; i &lt; 103; i++) {
          results103.push(i);
      }
  
      var fakeSearchForm = { $valid: true };
      var isSearchValidStub = sinon.stub(sut, "isSearchCriteriaValid").returns(true);
  
      var deferred = $q.defer();
      var promise = deferred.promise;
      var dsStub = sinon.stub(inSearchDataSvc, "getInsureds").returns(promise);
  
      var resolveStub = sinon.stub(deferred, "resolve");
  
      //how do i call resolve  and pass in results103
  
      sut.performSearch(fakeSearchForm);
  
      sinon.assert.calledOnce(isSearchValidStub);
      sinon.assert.calledOnce(dsStub);
  
      sinon.assert.called(resolveStub);
  
      expect(sut.searchResults.length).toBe(100);
  
  });
  </code></pre>