首页 > 解决方案 > 带有查询参数的 Testcafe 模拟请求/响应

问题描述

我正在尝试模拟使用查询参数的请求的响应。我已经在其他测试中使用过onrequestto,它可以无缝运行。已经有一个与我要问的问题类似的问题,但是无论我做什么,答案都对我不起作用:(

我的前端从后端获取数据的请求类似于

https://fake.com/search?Status=authorised&SortBy=Order

我在测试中所做的是:

import {RequestMock} from "testcafe";
const mock = RequestMock()
    .onRequestTo('\/search\?Status=authorised')
    .respond({data:ok}, 200, {'content-type': 'application/json charset=utf-8'})


fixture('Test suite')
    .page('fake.com')
    .requestHooks(mock);

并继续我的.test。OPTIONS 和 GET 请求都不会返回我期望的数据。我究竟做错了什么?

标签: javascripttestingmockingautomated-teststestcafe

解决方案


onRequestTo应该接收正则表达式值。因此,您必须将其更改为onRequestTo(/\/search\?Status=authorised/).

https://testcafe.io/documentation/402763/reference/test-api/requestmock/onrequestto


推荐阅读