正确的测试应该如何?

发布于 2025-02-13 08:40:27 字数 192 浏览 1 评论 0原文

我正在寻找对React编写正确且易于理解的测试。我读了很多文章和文章。但是我还没有理解一些问题。我有一个与Axios请求的组件。因此,有诸如get,张贴之类的请求。

后端在前端测试期间应该如何表现?前端测试开始后,后端切换到测试模式吗?每个前端测试后,数据库本身是否应该重置?

我认为我不应该模拟API检查组件是否正确渲染。正确的逻辑是什么?

I am searching to write correct and understandable tests on react. I have read a lot of articles and texts. But I haven't understood a few matters. I have a component which request with axios. So there are requests like GET, POST.

How should the backend behave during frontend testing? Should the backend switch to test mode after frontend tests started ? Should the database reset itself after each frontend test ?

I think I shouldn't mock API to check if component correct render. What is the correct logic ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来是傀儡 2025-02-20 08:40:28

我接近这将是嘲笑API调用的方式,这样做的原因是,我正在减少测试范围,因为我正在测试React部分,我会模拟成功响应并模拟错误响应并检查我的组件的工作原理。

对于API测试,我将在后端侧创建一个单独的测试用例。这样,就会分开关注。那是当我测试React方面,我仅将自己限制在React方面,这也将使您的测试速度快速进行嘲笑,因此不会发送实际请求,因此您将迅速得到响应。

The way that I approach this will be to mock the API call, the reason to do that will be, that I am reducing my scope of testing, As I am testing the React part, I would mock the success response and mock the error response and check how my components are working.

For the API testing, I will create a separate test case on the backend side. This way there will separation of concern. That is when I am testing react side I limit myself to the react side only and also this will make your test run fast as you are mocking the response, the actual request will not be sent and so you will get the response quickly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文