2021-05-05 10:13:49 -07:00
|
|
|
---
|
|
|
|
id: 587d824f367417b2b2512c5b
|
|
|
|
title: 使用 Chai-HTTP IV 的 PUT 方法對 API 響應運行功能測試
|
|
|
|
challengeType: 2
|
|
|
|
forumTopicId: 301591
|
|
|
|
dashedName: run-functional-tests-on-an-api-response-using-chai-http-iv---put-method
|
|
|
|
---
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
2021-09-03 01:11:45 -07:00
|
|
|
請注意,本項目在 [這個 Replit 項目](https://replit.com/github/freeCodeCamp/boilerplate-mochachai) 的基礎上進行開發。你也可以從 [GitHub](https://github.com/freeCodeCamp/boilerplate-mochachai/) 上克隆。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
2021-09-03 01:11:45 -07:00
|
|
|
這個練習與上一個類似。
|
|
|
|
|
|
|
|
現在你知道如何測試一個 `PUT` 請求了,輪到你從頭開始做了。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
2021-09-03 01:11:45 -07:00
|
|
|
在 `tests/2_functional-tests.js` 中,更改 `'Send {surname: "da Verrazzano"}'` 測試(`// #4`)並使用 `put` 和 `send` 方法來測試 `'/travellers'` 端點。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
2021-09-03 01:11:45 -07:00
|
|
|
在你的 PUT 請求中發送以下 JSON 對象。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"surname": "da Verrazzano"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-09-03 01:11:45 -07:00
|
|
|
在 `request.end` 的返回中檢查以下情況:
|
2021-05-05 10:13:49 -07:00
|
|
|
|
2021-09-03 01:11:45 -07:00
|
|
|
1. `status` 應該是 `200`
|
|
|
|
2. `type` 應該是 `application/json`
|
|
|
|
3. `body.name` 應該是 `Giovanni`
|
|
|
|
4. `body.surname` 應該是 `da Verrazzano`
|
2021-05-05 10:13:49 -07:00
|
|
|
|
2021-09-03 01:11:45 -07:00
|
|
|
請按照以上順序書寫斷言,順序錯誤會影響系統判定。 此外,請確保在完成後刪除 `assert.fail()`。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
2021-09-03 01:11:45 -07:00
|
|
|
應通過所有測試。
|
2021-05-05 10:13:49 -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-09-03 01:11:45 -07:00
|
|
|
應該測試 `res.status` 爲 200。
|
2021-05-05 10:13:49 -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-09-03 01:11:45 -07:00
|
|
|
應該測試 `res.type` 是否爲 `'application/json'`。
|
2021-05-05 10:13:49 -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-09-03 01:11:45 -07:00
|
|
|
應該測試 `res.body.name` 是否爲 `'Giovanni'`
|
2021-05-05 10:13:49 -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-09-03 01:11:45 -07:00
|
|
|
應該測試 `res.body.surname` 是否爲 `'da Verrazzano'`
|
2021-05-05 10:13:49 -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.
|
|
|
|
*/
|
|
|
|
```
|