如何测试promise返回功能 [英] how to test promise returning functions

查看:96
本文介绍了如何测试promise返回功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下异步函数返回Promise.

I have following async function that return Promise.

static getAccessToken(env: DeploymentEnv, username: string, password: string): Promise<AccessToken>;

现在,这是我为此编写的单元测试.

Now, this the unit test that I wrote for it.

it("should be able to get access token",async ()=>{
    let accessToken = await IModelHubServiceBusClient.getAccessToken('QA',
                      'abc@xyz.com',
                      'abc')!;

    assert.exists(accessToken);
});

运行时,它失败并显示以下错误:

When run it, it fails the test saying the following error:

should be able to get access token:
 Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

我在做什么错,任何建议将不胜感激. 提前致谢.

What Am I doing wrong, any suggestions will be appreciated. Thanks in advance.

推荐答案

如果您测试异步代码,则需要使用done回调

You need to use done callback if you test async code

it("should be able to get access token",async (done)=>{
    let accessToken = await IModelHubServiceBusClient.getAccessToken('QA',
                      'bistroDEV_pmadm1@mailinator.com',
                      'pmadm1')!;

    assert.exists(accessToken);
    done();
});

这篇关于如何测试promise返回功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆