Javascript 如何模拟getPass函数的响应

Javascript 如何模拟getPass函数的响应,javascript,node.js,mocha.js,sinon,chai,Javascript,Node.js,Mocha.js,Sinon,Chai,您好,我正在编写javascript函数的单元测试用例。我用的是摩卡咖啡、柴、expect、西农 app.js module.exports = { saveInGlobal: async () => { try { if (global.pass !== null && global.pass !== '') { return module.exports.getpass().then((res) => {

您好,我正在编写javascript函数的单元测试用例。我用的是摩卡咖啡、柴、expect、西农

app.js

module.exports = {
    saveInGlobal: async () => {
      try {
        if (global.pass !== null && global.pass !== '') {
          return module.exports.getpass().then((res) => {
            return global.pass = res;
          });
        }
      } catch (err) {

      }
    },
    getPass: async () => {
       return "test";
    } 
}

我想模拟
getPass()
函数,然后断言
global.pass
。有人能建议如何在这里使用
sinon

模拟
getPass()
吗?基本功能由
sinon.stub()
提供,它覆盖对象中的某些属性-在您的例子中是
模块.getPass

const theModule = require('theModule'); // module with getPass
it('foo', () => {
    // Prepare
    const getPassStub = sinon.stub(theModule, 'getPass');
    getPassStub.resolves('mocked');
    // Act

    // call your code here

    // Assert
    expect(getPassStub.calledOnce).to.be.true;
    getPassStub.restore()
});
这是可行的,但只有在测试通过的情况下。在失败的情况下,存根仍然存在(
restore
未被调用),并且
getPass
在测试执行的其余部分保持模拟状态

考虑使用:
after(()=>module.getPass.restore())
或者更优选地使用
sinon.Sandbox

存根单据:


沙盒文档:

您不能将
try/catch
与异步代码一起使用(
promise.then
)。所以我把它去掉了。下面是单元测试:

app.js

module.exports={
savinglobal:async()=>{
if(global.pass!==null&&global.pass!==“”){
返回module.exports.getPass()。然后((res)=>{
返回(global.pass=res);
});
}
},
getPass:async()=>{
返回“测试”;
},
};
app.test.js

const-app=require(“./app”);
const sinon=要求(“sinon”);
const{expect}=require(“chai”);
描述(“49858991”,()=>{
之后(()=>{
sinon.restore();
});
描述(#savinglobal),()=>{
它(“应该保存在全局”,异步()=>{
global.pass=“a”;
const getPassStub=sinon.stub(应用程序,“getPass”)。解析(“模拟值”);
等待app.saveingglobal();
sinon.assert.calledOnce(getPassStub);
expect(global.pass.to.be.eq(“模拟值”);
});
它(“应该什么都不做”,async()=>{
global.pass=null;
const getPassStub=sinon.stub(应用程序,“getPass”)。解析(“模拟值”);
等待app.saveingglobal();
sinon.assert.notCalled(getPassStub);
expect(global.pass).to.be.eq(null);
});
});
描述(“#getPass”,()=>{
它('应该返回“test”,async()=>{
const actual=wait app.getPass();
期望(实际)成为eq(“测试”);
});
});
});
100%覆盖率的单元测试结果:

49858991
#拯救全球
✓ 应该在全球范围内节约
✓ 什么也不做
#通行证
✓ 应返回“测试”
3次通过(16毫秒)
-------------|----------|----------|----------|----------|-------------------|
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s|
-------------|----------|----------|----------|----------|-------------------|
所有文件| 100 | 100 | 100 | 100 ||
app.js | 100 | 100 | 100 | 100 ||
app.test.js | 100 | 100 | 100 | 100 ||
-------------|----------|----------|----------|----------|-------------------|

源代码:

我得到一个错误
模块.exports.getpass(…)。then不是一个函数
我忽略了
then
。当您在
getPass
上调用时,代码中出现了错误,它不会返回承诺。要么使
getPass
async(并返回承诺并在存根上使用
stub.resolves(value)
),要么从目标代码中删除
then
,并以“同步”的方式处理。我的错,它是异步的。我仍然收到相同的错误
module.exports.getpass(…)。那么它不是一个函数
请仔细阅读您的代码。您可能误读了代码> GETPASS <代码> >代码> GETPASS。请考虑使用Leter或更好的TypeScript。