剧作家拦截服务器端网络请求

问题描述 投票:0回答:2

找不到任何关于如何使用剧作家模拟/存根服务器端请求的好文档。

一个例子是拦截 nextjs 中的 getServerSideProps:点击路由使服务器发出请求(数据库 API 等)。然后它可以执行一些业务逻辑(也应该通过测试涵盖),然后将其作为 props 传递给组件,然后发送到客户端(在服务器端渲染)。

我希望找到答案,即在不将一些测试逻辑混合到业务逻辑中的情况下模拟数据库 API 请求。

testing mocking playwright stubbing interception
2个回答
0
投票

Playwright 允许您进行拦截和模拟/存根。 UI操作可以触发API调用,并且无需发送请求即可拦截 回应。

您也可以使用 moks 和存根。

const mock = { animals: [] }

await page.route('**/Zoo/v1/books', (animals) => 
    route.fulfill({
        status: 304,
        body: JSON.stringify(mock),
    })),
);

await page.goto('https://www.demoqa/animals');

查看更多https://github.com/microsoft/playwright/issues/1774#issuecomment-769247500

https://playwright.dev/docs/next/network#modify-responses


0
投票

Dominik Ferber 对使用带有页面路由器的 Next.js 应用程序的 MSW 模拟服务器端请求有详细的解释 - https://frontend-digest.com/using-playwright-to-test-next-js-applications- 80a767540091

注意:这篇文章仅供会员阅读,但由 Michael Gwynne 重写,地址为 https://dev.to/votemike/server-side-mocking-for-playwright-in-nextjs-using-mock-service-工人-4p6gMic

© www.soinside.com 2019 - 2024. All rights reserved.