0

i am not able get the resolved value in the response the promise stub returns undefined

   otpHelper.sendOtp = (number) => {
       console.log('number', number);
  return Promise.resolve({ status: '0', requestId: 'hdgfhsgdfdsgfjsgdfj' });
};

sendOtpStub = sinon.stub(otpHelper, 'sendOtp');
const otpHelperStub = proxyquire('../routes/register', {
  '../utils/otpHelper': {
    sendOtp: sendOtpStub,
    // checkOtp: checkOtpStub,
  },
});

sendOtpStub is called but the value that i get is undefined i need to get the resolved value from the promise.its resolving to undefined am using if am not using the stub its working as expected. if i put sendOtp: otpHelper.sendOtp its working as expected but when i make it as a stub its not returning the resolved value.

zabusa
  • 2,520
  • 21
  • 25

1 Answers1

0

A stub does not call the original method, that's how it's supposed to work. What you want is a spy, which wraps the original method and invokes it when called.

sinon.spy(otpHelper, 'sendOtp')
Lennholm
  • 7,205
  • 1
  • 21
  • 30
  • Actually I don't want to call the original .it's a API call so I created a spicy and change according to inputs – zabusa Oct 21 '18 at 17:05
  • @zabusa The "original" in this case is the `otpHelper.sendOtp` method you're creating a few lines above, which return value you state that you do want. If you're talking about the original `otpHelper` *object*, you're already mocking that with proxyquire. – Lennholm Oct 21 '18 at 17:15
  • Thanks for the answer. actually i need this like. i made a desent working model.but something still worrying me. i made some changes.`sendOtpStub = sinon.stub();` and in every `it` condition i need to put `resolves` and `rejects` with some different values. – zabusa Oct 21 '18 at 17:19
  • @Lenholm i was wrong getting the concept of stub.thank you for the enlightenment.i directly run into code without reading the documentation – zabusa Oct 21 '18 at 18:51