【问题标题】:Mocha chai resolve multiple promises摩卡柴解决多个承诺
【发布时间】:2019-11-15 14:25:24
【问题描述】:

我正在尝试对这个 Promise 进行测试,但我收到了这个错误:

“错误:超过 2000 毫秒的超时。对于异步测试和钩子,请确保调用了“done()”;如果返回 Promise,请确保它已解决。(C:\Users\Ranieri\Documents\Projetos\Node Js\ testestest\test\libs\registerUser.test.js)"

我已经增加了超时时间,但仍然没有解决问题。

我在stackoverflow上搜索了这里的异步测试,没有发现任何类似的东西或任何人

我的测试代码https://github.com/ran-j/teste

已经试过了:

expect(Promise.resolve( userPromesie.selectUser(rUser) ) ).to.be.null

return expect(Promise.resolve( userPromesie.selectUser(rUser) ) ).to.be.null

userPromesie.selectUser(rUser).then((result) => result.to.be.null

【问题讨论】:

    标签: node.js asynchronous mocha.js tdd chai


    【解决方案1】:

    selectUser 返回一个Promise,您需要等待它解决才能对它进行断言。这可以通过两种方式实现:

    • 在返回的 Promise 上使用 then,在该块中调用 done() 函数。
    • 使用async/await,你声明你的函数async和你await Promise

    以下是async/await 方法的示例:

    it('should do something with the user', async () => {
        const user = await userPromise.selectUser(rUser);
        expect(user).to.be.null;
    });
    

    我强烈建议您阅读 Promises 以了解更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

    编辑下面是使用 done 方法的示例:

    it('should do something with the user', (done) => {
        userPromise.selectUser(rUser).then((user) => {
            expect(user).to.be.null;
            done();
        });
    
    });
    

    【讨论】:

    • 同样的错误......,我知道 promesis 是如何工作的,然后我尝试了,最后......
    • 我添加了一个使用done 方法的示例。希望这对你有用!
    猜你喜欢
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 2018-05-24
    • 2015-05-01
    • 1970-01-01
    • 2018-10-02
    相关资源
    最近更新 更多