respond
Always respond with same overwrite.
Usage
mock.respond(overwrites, { header, statusCode, fetchResponse })
Parameters
Name | Type | Details |
---|---|---|
overwrites | MockOverwrite | payload to overwrite the response |
params optional | MockResponseParams | additional respond parameters to overwrite |
params.header optional | Object | overwrite specific headers |
params.statusCode optional | Number | overwrite response status code |
params.fetchResponse optional | Boolean | fetch real response before responding with mocked data |
Example
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
})
})