小编Bhu*_*wan的帖子

使用 Sinon 和 Mocha 的存根实例方法

我已经为下面的函数编写了测试用例(我省略了很多不必要的代码,只提供了必要的东西,但如果你需要任何其他信息,请告诉我)。

static getLibs() {
  return new Promise((resolve, reject) => {
    const instance = new LibClass();
    instance.loadLibs().then((libs) => {
      if (!libs) {
        return LibUtils.createLib();
      } else {
        return Promise.resolve([]);
      }
    }).then(resolve).catch((err) => {
      //log stuff here
    })
  })
}

export default class LibClass {
  //constructor
  //method
  createLib() {
    return new Promise(() => {
      //some stuff
    })
  }
}
Run Code Online (Sandbox Code Playgroud)

describe('Library', () => {
  it('should get libs', () => {
    let obj = new LibClass();
    let mstub = sinon.stub(obj, 'loadLibs').returns(Promise.resolve('success'));

    return LibWrapper.getLibs().then((res) => …
Run Code Online (Sandbox Code Playgroud)

unit-testing mocha.js node.js sinon chai

2
推荐指数
1
解决办法
3850
查看次数

标签 统计

chai ×1

mocha.js ×1

node.js ×1

sinon ×1

unit-testing ×1