开玩笑如何测试Express API POST请求?

用户9132502

我需要测试对端点的POST请求是否可以通过Jest测试正常工作。我的想法是先获取我的Services表的计数(我正在使用sequelize orm),然后发送新的发帖请求并最终获取新的计数,并比较旧计数+ 1是否等于新计数,如果为true,那么POST请求就可以正常工作。

test('Create a valid Service', async (done) => {
const service = {
    name: "cool",
    description: "description"
};

await Service.count().then(async function (count) {

    await request(app)
        .post('/api/services')
        .send(service)
        .then(async () => {
            await Service.count().then(function (newcount) {
                expect(newcount).toBe(count + 1);
            });
        })
        .catch(err => console.log(`Error ${err}`));
});

});

对我来说,测试看起来不错,但是当我运行它时,我得到:

Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

是否缺少某些东西,或者还有更好的方法来测试POST请求?和开玩笑吗?

上流

这是因为您没有调用jest回调函数中传递完成回调。可以这样做。

test('Create a valid Service', async(done) => {
    const service = {
        name: "cool",
        description: "description"
    };

    await Service.count().then(async function (count) {

        await request(app)
            .post('/api/services')
            .send(service)
            .then(async() => {
                await Service.count().then(function (newcount) {
                    expect(newcount).toBe(count + 1);
                    // execute done callback here
                    done();
                });
            })
            .catch(err => {
                // write test for failure here
                console.log(`Error ${err}`)
                done()
            });
    });
});

您还可以通过这种方式编写此代码,以便提高可读性并最大程度地使用async / await

test('Create a valid Service', async(done) => {
    const service = {
        name: "cool",
        description: "description"
    };
    try {
        const count = await Service.count();
        await request(app).post('/api/services').send(service)
        const newCount = await Service.count()
        expect(newCount).toBe(count + 1);
        done()
    } catch (err) {
        // write test for failure here
        console.log(`Error ${err}`)
        done()
    }
});

默认情况下,Jest还可以在async / await情况下解析promise我们也可以不用回调函数来实现

test('Create a valid Service', async() => {
    const service = {
        name: "cool",
        description: "description"
    };
    try {
        const count = await Service.count();
        await request(app).post('/api/services').send(service)
        const newCount = await Service.count()
        expect(newCount).toBe(count + 1);
    } catch (err) {
        // write test for failure here
        console.log(`Error ${err}`)
    }
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用sinon嘲笑POST请求的访存API,为react.js应用程序开玩笑

如何模拟axios API调用?开玩笑

如何使用Servlet API处理POST请求

如何通过API提交POST请求

如何使用Rails从API获取POST请求

开玩笑:对讲机测试API方法

如何开玩笑地测试与第三方API一起使用的自定义回调?

无法在Laravel测试中对API路由执行POST请求

Laravel POST api 请求为空。如何在laravel中发出post请求

无法使用Express API发出POST或GET请求

TypeORM/Express 路由器 API 响应 404 POST 请求

如何在春季使用 POST 请求实现搜索 API

如何从ReactJS Web应用发送POST请求到Django API?

如何解决与 rest api POST 请求相关的异常?

如何使用POST请求保存值数组?(Wordpress API)

如何向外部API发出GET和POST请求?

如何使用Django REST API验证android用户POST请求?

如何在Flutter中针对Web API发出POST请求

如何根据API类型使用BODY发送POST或GET请求

如何在 PHP 中发出这个 api post 请求?

如何向mailchimp的Axios post请求添加API密钥

ReactJS:如何使用 POST 请求更新 API 中的布尔字段

如何在 POST API 中创建请求 ID 并记录它

如何开玩笑地测试

如何使用Resty创建自动化API POST测试请求

如何从 Express API POST 请求正文中检索 excel .xlsx 数据?

我怎样才能测试其中带有api调用的开玩笑的vuex动作?

反应开玩笑的测试。无法使用Google JS API读取未定义的属性“地图”

使用Fetch API的POST请求?