2021-06-15 00:49:18 -07:00
---
id: 587d824f367417b2b2512c5b
2021-07-30 01:41:44 +09:00
title: Executar testes funcionais em uma resposta de API usando Chai-HTTP IV - método PUT
2021-06-15 00:49:18 -07:00
challengeType: 2
forumTopicId: 301591
dashedName: run-functional-tests-on-an-api-response-using-chai-http-iv---put-method
---
# --description--
2021-07-30 23:57:21 +09:00
Lembrando que este projeto está sendo construído a partir do [Replit ](https://replit.com/github/freeCodeCamp/boilerplate-mochachai ), ou pose ser clonado no [GitHub ](https://github.com/freeCodeCamp/boilerplate-mochachai/ ). Este exercício é semelhante ao anterior. Vejamos mais detalhes aqui.
2021-06-15 00:49:18 -07:00
2021-07-30 01:41:44 +09:00
Agora que vimos como é feito, é a sua vez de fazer do zero.
2021-06-15 00:49:18 -07:00
# --instructions--
2021-07-30 01:41:44 +09:00
Dentro de `tests/2_functional-tests.js` , altere o teste `'send {surname: "da Verrazzano"}'` test (`// #4` ):
2021-06-15 00:49:18 -07:00
2021-07-30 01:41:44 +09:00
Envie a resposta em JSON a seguir como payload para a rota `/travellers` :
2021-06-15 00:49:18 -07:00
```json
{
"surname": "da Verrazzano"
}
```
2021-07-30 01:41:44 +09:00
Verifique o seguinte, dentro de uma função de callback de `request.end` :
2021-06-15 00:49:18 -07:00
1. `status`
2. `type`
3. `body.name`
4. `body.surname`
2021-07-30 01:41:44 +09:00
Siga a ordem de declarações acima, pois dependemos disso. Não se esqueça de remover `assert.fail()` assim que o teste terminar.
2021-06-15 00:49:18 -07:00
# --hints--
2021-07-30 01:41:44 +09:00
Todos os testes devem passar
2021-06-15 00:49:18 -07:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=3').then(
(data) => {
assert.equal(data.state, 'passed');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2021-07-30 01:41:44 +09:00
Você deve testar se 'res.status' será 200
2021-06-15 00:49:18 -07:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=3').then(
(data) => {
assert.equal(data.assertions[0].method, 'equal');
assert.equal(data.assertions[0].args[0], 'res.status');
assert.equal(data.assertions[0].args[1], '200');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2021-07-30 01:41:44 +09:00
Você deve testar se 'res.type' será 'application/json'
2021-06-15 00:49:18 -07:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=3').then(
(data) => {
assert.equal(data.assertions[1].method, 'equal');
assert.equal(data.assertions[1].args[0], 'res.type');
assert.match(data.assertions[1].args[1], /('|")application\/json\1/);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2021-07-30 01:41:44 +09:00
Você deve testar se 'res.body.name' será 'Giovanni'
2021-06-15 00:49:18 -07:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=3').then(
(data) => {
assert.equal(data.assertions[2].method, 'equal');
assert.equal(data.assertions[2].args[0], 'res.body.name');
assert.match(data.assertions[2].args[1], /('|")Giovanni\1/);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2021-07-30 01:41:44 +09:00
Você deve testar se 'res.body.surname' será 'da Verrazzano'
2021-06-15 00:49:18 -07:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=3').then(
(data) => {
assert.equal(data.assertions[3].method, 'equal');
assert.equal(data.assertions[3].args[0], 'res.body.surname');
assert.match(data.assertions[3].args[1], /('|")da Verrazzano\1/);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
# --solutions--
```js
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
```