Files
freeCodeCamp/curriculum/challenges/chinese-traditional/06-quality-assurance/quality-assurance-and-testing-with-chai/run-functional-tests-on-api-endpoints-using-chai-http-ii.md

76 lines
2.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
id: 587d824f367417b2b2512c59
title: 使用 Chai-HTTP II 在 API 端上運行功能測試
challengeType: 2
forumTopicId: 301592
dashedName: run-functional-tests-on-api-endpoints-using-chai-http-ii
---
# --description--
請注意,本項目在[這個 Replit 項目](https://replit.com/github/freeCodeCamp/boilerplate-mochachai)的基礎上進行開發。你也可以從 [GitHub](https://repl.it/github/freeCodeCamp/boilerplate-mochachai) 上克隆。
# --instructions--
`tests/2_functional-tests.js` 中,修改 `'Test GET /hello with your name'` 測試(`// #2`),對 `status``text` 斷言。
在查詢中發送 name`?name=<your_name>` 添加到路由。 端點響應 `'hello <your_name>'`
# --hints--
應通過所有測試。
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional&n=1').then(
(data) => {
assert.equal(data.state, 'passed');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
應測試 “res.status” 是否爲 200。
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional&n=1').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);
}
);
```
應測試 “res.text“ 是否爲 ”hello Guest“。
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional&n=1').then(
(data) => {
assert.equal(data.assertions[1].method, 'equal');
assert.equal(data.assertions[1].args[0], 'res.text');
assert.match(data.assertions[1].args[1], /hello [\w\d_-]/);
},
(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.
*/
```