gpt4 book ai didi

javascript - stub 不返回 proxyquire 中的解析数据变得未定义

转载 作者:行者123 更新时间:2023-12-03 00:54:03 26 4
gpt4 key购买 nike

我无法在 Promise stub 返回的响应中获取解析值未定义

   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 被调用,但我得到的值是 undefined 我需要从 Promise 中获取解析值。其解析为 undefined如果我不使用 stub ,那么它会按预期工作。如果我输入 sendOtp: otpHelper.sendOtp 它会按预期工作,但是当我将其作为 stub 时,它不会返回解析值。

最佳答案

stub 不会调用原始方法,这就是它应该如何工作的。您想要的是一个 spy ,它包装原始方法并在调用时调用它。

sinon.spy(otpHelper, 'sendOtp')

关于javascript - stub 不返回 proxyquire 中的解析数据变得未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52917300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com