简体   繁体   English

为什么 sinon 存根返回未定义?

[英]why sinon stub is returning undefined?

I am trying to stub a library function with Sinon.我正在尝试使用 Sinon 存根库 function。 Sorry can't post exact code but it looks something like the code below.抱歉无法发布确切的代码,但它看起来像下面的代码。

The library I am trying to stub library.js我试图存根 library.js 的库

async function a(input) {
    // make some api calls here
    returns input;
}
export async function b {
    const output = await a(123);
    // do some magic here with output
    // call again a
    returns a(output);
}

My test case file: sinon.js我的测试用例文件:sinon.js

import * as lib from 'library.js'

describe('', ()=>{
    it('', ()=>{
        sinon.stub(lib, 'b')
        .withArgs(123)
        .resolves(123)
    })
})

when I run this code my stub returns undefined instead of expected output which 123 Can anyone help me find what's wrong I am doing here?当我运行此代码时,我的存根返回undefined而不是预期的 output which 123谁能帮我找出我在这里做错了什么?

I do not claim to be an expert on this subject but it seems to me you are trying to stub instead of mock.我并不声称自己是该主题的专家,但在我看来,您正在尝试存根而不是模拟。 Try something like this.尝试这样的事情。

describe('', () => {
  it('', () => {
    const mock = sinon.mock(lib);
    mock
      .expects('b').withArgs(123)
      .resolves(345);
    lib.b(123).then((c) => console.log(c));
  })
})

This will print 345. Pretty sure there is a more elegant way but this will work.这将打印 345。很确定有一种更优雅的方式,但这会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM