2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: 587d824e367417b2b2512c58
|
2021-03-22 13:52:28 +00:00
|
|
|
title: 使用 Chai-HTTP 在 API 端上运行功能测试
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 2
|
2020-09-17 10:50:23 +00:00
|
|
|
forumTopicId: 301593
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: run-functional-tests-on-api-endpoints-using-chai-http
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
|
|
|
|
2021-05-12 15:55:58 +00:00
|
|
|
请注意,本项目在[这个 Replit 项目](https://replit.com/github/freeCodeCamp/boilerplate-mochachai)的基础上进行开发。你也可以从 [GitHub](https://repl.it/github/freeCodeCamp/boilerplate-mochachai) 上克隆。
|
2021-02-06 04:42:36 +00:00
|
|
|
|
2021-09-03 08:11:45 +00:00
|
|
|
Mocha 允许你使用名为 `chai-http` 的插件测试异步操作,例如调用 API 端点。
|
2021-02-06 04:42:36 +00:00
|
|
|
|
2021-09-03 08:11:45 +00:00
|
|
|
以下是使用 `chai-http` 测试名为 `'GET /hello?name=[name] => "hello [name]"'` 的套件的示例:
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
suite('GET /hello?name=[name] => "hello [name]"', function () {
|
2021-09-03 08:11:45 +00:00
|
|
|
test('?name=John', function (done) {
|
2021-02-06 04:42:36 +00:00
|
|
|
chai
|
|
|
|
.request(server)
|
2021-09-03 08:11:45 +00:00
|
|
|
.get('/hello?name=John')
|
2021-02-06 04:42:36 +00:00
|
|
|
.end(function (err, res) {
|
2021-09-03 08:11:45 +00:00
|
|
|
assert.equal(res.status, 200, 'Response status should be 200');
|
|
|
|
assert.equal(res.text, 'hello John', 'Response should be "hello John"');
|
2021-02-06 04:42:36 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2021-09-03 08:11:45 +00:00
|
|
|
});
|
2021-02-06 04:42:36 +00:00
|
|
|
```
|
|
|
|
|
2021-09-03 08:11:45 +00:00
|
|
|
该测试向服务器发送一个 `GET` 请求,并将名称作为 URL 查询字符串(`?name=John`)。 在`end` 方法的回调函数中,接收到响应对象(`res`)并包含 `status` 属性。
|
|
|
|
|
|
|
|
第一个 `assert.equal` 检查状态是否为 `200`。 第二个 `assert.equal` 检查响应字符串(`res.text`)是否为 `"hello John"`。
|
|
|
|
|
|
|
|
同时,请注意测试的回调函数中的 `done` 参数。 在测试结束时,调用它且不带参数,是发出异步操作完成所必需的信号。
|
2020-09-17 10:50:23 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --instructions--
|
|
|
|
|
2021-09-03 08:11:45 +00:00
|
|
|
在 `tests/2_functional-tests.js` 中,修改 `'Test GET /hello with no name'` 测试(`// #1`),对响应的 `status` 和 `text` 使用断言来通过测试。 不要改变传递给断言的参数。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-09-03 08:11:45 +00:00
|
|
|
不应该有任何 URL 查询。 如果没有名称 URL 查询,端点将使用 `hello Guest` 进行响应。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-22 13:52:28 +00:00
|
|
|
应通过所有测试。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00: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 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-09-03 08:11:45 +00:00
|
|
|
应该测试 `res.status` 为 200。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00: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 22:03:03 +00:00
|
|
|
|
2021-09-03 08:11:45 +00:00
|
|
|
应该测试 `res.text` == `'hello Guest'`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00: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 22:03:03 +00:00
|
|
|
```
|
2020-08-13 15:24:35 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```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.
|
|
|
|
*/
|
|
|
|
```
|