NdaJunior
NdaJunior

Reputation: 374

Using Sinon to stub code inside a Promise and return a Resolve()

I've been stuck on this for the last day and I've decided that I need assistance. I've got the following javascript class method that I am writing a unit test for:

getUser = function(req, res){
  return new Promise(function(resolve, reject){
      User.getByUserId(user_id, function(err, user){
        if (err){
          return reject({ message: defaultErrorMessage })
        } else {
          return user ? resolve() : reject({ message: 'The specified user does not exist.' })
        }
      })
  }).then(function(){

        ....Promise Chain Continues...

})

Essentially what Im trying to do is write a unit test that stubs out the User.getByUserId mongoose call with the following:

let resolvedPromise = sinon.stub().resolves();
sinon.stub(User,'getByUserId').returns(resolvedPromise);

My thinking is that I want to stub out the DB call that finds a user and return a resolved promise so that the promise chain can continue, however my current solution doesn't allow the promise chain to continue.

How do I have a sinon stub within a promise and have that stub return a resolve() that allows the promise chain to continue? Help will be greatly appreciated!

Upvotes: 1

Views: 8813

Answers (1)

deerawan
deerawan

Reputation: 8443

User.getByUserId is a function with a callback. We couldn't use returns of sinon.stub for that. The solution is to use yields

sinon.stub(User, 'getByUserId').yields(null, userObj);

Reference:

https://sinonjs.org/releases/v7.2.2/stubs/#stubyieldsarg1-arg2-

Update

The difference between yields and returns can be described using example below:

// User.js

/** function with ordinary return **/
function getUser() {
  return { name: 'admin', sex: 'male' };
}

/** function with callback and no return **/
function getUserById(id, callbackFunc) {
  callbackFunc();
}

/** function that return promise **/
function getUserBySex(sex) {
  return getFromDb(); // getFromDb() is a promise function
}

And for sinon stub in test file

// test.js

// use `returns` for function with ordinary return
sinon.stub(User, 'getUser').returns({ name: 'whatever', sex: 'female' });

// use `yields` or `callArgsWith` for function that has callback function
sinon.stub(User, 'getUserById').yields(null, userObj);
sinon.stub(User, 'getByUserId').callsArgWith(1, null, userObj); // 1 => second parameter

// use `resolves` for function that returns promise
sinon.stub(User, 'getUserBySex').resolves(userObj);

More references:

Upvotes: 9

Related Questions