How does one stub promise with sinon?

后端 未结 5 2130
耶瑟儿~
耶瑟儿~ 2020-12-13 01:55

I have a data service with following function

function getInsureds(searchCriteria) {

    var deferred = $q.defer();

    insuredsSearch.get(searchCriteria,
         


        
5条回答
  •  感情败类
    2020-12-13 02:17

    I had a similar situation and the accepted answer and comments were not working, but along with this question they helped me solve this in the following way. I hope it is helpful for somebody.

    var Promise = require('bluebird');
    
    var deferred = Promise.defer();
    stub = sinon.stub(deferred, 'resolve').returns(deferred.promise);
    
    deferred.resolve({data: data});
    // or
    deferred.reject(new Error('fake error'));
    

提交回复
热议问题