跳至主要内容

响应

始终使用相同的覆盖内容进行响应。

用法
mock.respond(overwrites, { header, statusCode, fetchResponse })
参数
名称类型详情
覆盖MockOverwrite覆盖响应的有效负载
参数
可选
MockResponseParams要覆盖的其他响应参数
params.header
可选
对象覆盖特定标头
params.statusCode
可选
数字覆盖响应状态代码
params.fetchResponse
可选
布尔值在使用模拟数据进行响应之前获取真实响应
示例
respond.js
it('should demonstrate response overwrite with static data', async () => {
const mock = await browser.mock('https://todo-backend-express-knex.herokuapp.com/', {
method: 'get'
})

mock.respond([{
title: 'Injected (non) completed Todo',
order: null,
completed: false
}, {
title: 'Injected completed Todo',
order: null,
completed: true
}], {
statusCode: 200,
fetchResponse: true // default
})

await browser.url('https://todobackend.com/client/index.html?https://todo-backend-express-knex.herokuapp.com/')

await $('#todo-list li').waitForExist()
console.log(await $$('#todo-list li').map(el => el.getText()))
// outputs: "[ 'Injected (non) completed Todo', 'Injected completed Todo' ]"
})

it('should demonstrate response overwrite with dynamic data', async () => {
const mock = await browser.mock('https://todo-backend-express-knex.herokuapp.com/')

mock.respond((request) => {
if (request.body.username === 'test') {
return { ...request.body, foo: 'bar' }
}
return request.body
}, {
statusCode: () => 200,
headers: () => ({ foo: 'bar }),
fetchResponse: false // do not fetch real response
})
})

欢迎!我怎样才能帮助您?

WebdriverIO AI Copilot