3.5 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d824f367417b2b2512c5b | Eseguire test funzionali su una risposta API utilizzando il metodo PUT di Chai-HTTP IV | 2 | 301591 | run-functional-tests-on-an-api-response-using-chai-http-iv---put-method |
--description--
Come promemoria, questo progetto verrà costruito a partire dalla seguente bozza su Replit, o clonato da GitHub.
Questo esercizio è simile a quello precedente.
Ora che sai come testare una richiesta PUT
, è il tuo turno di farlo da zero.
--instructions--
All'interno di tests/2_functional-tests.js
, cambia il test 'Send {surname: "da Verrazzano"}'
(// #4
) e usa i metodi put
e send
per testare l'endpoint '/travellers'
.
Invia il seguente oggetto JSON con la tua richiesta PUT:
{
"surname": "da Verrazzano"
}
Controlla quanto segue, all'interno della callback request.end
:
- Lo
status
dovrebbe essere200
- Il
type
dovrebbe essereapplication/json
- Il
body.name
dovrebbe essereGiovanni
- Il
body.surname
dovrebbe essereda Verrazzano
Segui l'ordine di asserzione indicato sopra - facciamo affidamento su di esso. Inoltre, assicurati di rimuovere assert.fail()
una volta completato.
--hints--
Tutti i test dovrebbero passare
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional&n=3').then(
(data) => {
assert.equal(data.state, 'passed');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
Dovresti verificare che res.status
sia 200
(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);
}
);
Dovresti verificare che res.type
sia 'application/json'
(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);
}
);
Dovresti verificare che res.body.name
sia 'Giovanni'
(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);
}
);
Dovresti verificare che res.body.surname
sia 'da Verrazzano'
(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--
/**
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.
*/