响应
始终使用相同的覆盖内容进行响应。
用法
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
})
})