【问题标题】:Unit Test Function which Contains Promises包含 Promise 的单元测试函数
【发布时间】:2019-10-17 01:01:39
【问题描述】:

我有这个功能:

function _init()
{ 
    return _getNetDesc() // this is a promise
    .then(data => {

       // do something
    })  
    .then(() => {

        return _getNetOperation(); // this is a promise
    })
    .then( () =>{
        return _getNetNodeList(); // this is a promise
    }) 

    .catch( e =>
    {
        logger.error("JZW","init",e);

    });
}

为了测试这个函数是否抛出,我写在mocha/sinon/chai:

it("should throw eception for no GW", async () => {
        _getNetDesc = sinon.stub().throws();
        const test = await jzw.init;
        expect(test).to.be.rejected;

    });

但我明白了

 TypeError: [Function: _init] is not a thenable.

【问题讨论】:

    标签: node.js unit-testing promise chai sinon


    【解决方案1】:

    你错过了调用函数并拒绝承诺:

    it("should throw eception for no GW", async () => {
      _getNetDesc = sinon.stub().returns(Promise.reject(...)); // returns a rejected promise
      const test = await jzw.init(); // call the function to get the promise
      expect(test).to.be.rejected;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-25
      • 1970-01-01
      • 2021-06-23
      • 2021-12-23
      • 1970-01-01
      • 2018-05-13
      • 1970-01-01
      相关资源
      最近更新 更多