2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d824e367417b2b2512c58
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: 使用 Chai-HTTP 进行 API 功能测试 (1)
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 2
|
2020-09-17 03:50:23 -07:00
|
|
|
|
forumTopicId: 301593
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
|
|
|
|
请注意,本项目在 [这个 Repl.it 项目](https://repl.it/github/freeCodeCamp/boilerplate-mochachai) 的基础上进行开发。你也可以从 [GitHub](https://repl.it/github/freeCodeCamp/boilerplate-mochachai) 上克隆。
|
2020-09-17 03:50:23 -07:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --instructions--
|
|
|
|
|
|
|
|
|
|
测试 status 和 `text.response`。替换 `assert.fail()` 使测试通过。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-09-17 03:50:23 -07:00
|
|
|
|
不要在 query 中传入 name,后端将会返回 "hello Guest"。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
不应有未通过的测试
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
(getUserInput) =>
|
|
|
|
|
$.get(getUserInput('url') + '/_api/get-tests?type=functional&n=0').then(
|
|
|
|
|
(data) => {
|
|
|
|
|
assert.equal(data.state, 'passed');
|
|
|
|
|
},
|
|
|
|
|
(xhr) => {
|
|
|
|
|
throw new Error(xhr.responseText);
|
|
|
|
|
}
|
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
你需要测试 'res.status' 是否为 200
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
(getUserInput) =>
|
|
|
|
|
$.get(getUserInput('url') + '/_api/get-tests?type=functional&n=0').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);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
你需要测试 'res.text' 是否为 'hello Guest'
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
(getUserInput) =>
|
|
|
|
|
$.get(getUserInput('url') + '/_api/get-tests?type=functional&n=0').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 Guest\1/);
|
|
|
|
|
},
|
|
|
|
|
(xhr) => {
|
|
|
|
|
throw new Error(xhr.responseText);
|
|
|
|
|
}
|
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|