admin管理员组

文章数量:1352840

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!

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!

Share Improve this question edited Jan 23, 2019 at 10:13 NdaJunior asked Jan 23, 2019 at 10:05 NdaJuniorNdaJunior 3743 silver badges17 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

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/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:

  • https://sinonjs/releases/v7.2.2/stubs/#stubcallsargwithindex-arg1-arg2-
  • https://sinonjs/releases/v7.2.2/stubs/#stubresolvesvalue

本文标签: javascriptUsing Sinon to stub code inside a Promise and return a Resolve()Stack Overflow