Files
freeCodeCamp/curriculum/challenges/chinese-traditional/06-quality-assurance/quality-assurance-and-testing-with-chai/test-if-a-value-falls-within-a-specific-range.md

88 lines
2.3 KiB
Markdown

---
id: 587d824c367417b2b2512c4f
title: 測試某個值是否在特定範圍內
challengeType: 2
forumTopicId: 301598
dashedName: test-if-a-value-falls-within-a-specific-range
---
# --description--
請注意,本項目在[這個 Replit 項目](https://replit.com/github/freeCodeCamp/boilerplate-mochachai)的基礎上進行開發。你也可以從 [GitHub](https://repl.it/github/freeCodeCamp/boilerplate-mochachai) 上克隆。
```javascript
.approximately(actual, expected, delta, [message])
```
斷言 `actual` 等於 `expected`,在 +/- `delta` 的範圍內。
# --instructions--
`tests/1_unit-tests.js` 中,在 `Comparisons` 套件裏標有 `#10` 的測試中,將每個 `assert` 改成 `assert.approximately`,讓測試通過(結果應該返回 `true`)。
選擇最小範圍(第三個參數)來通過所有測試。 它應該小於 1。
# --hints--
不應有未通過的測試
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=unit&n=9').then(
(data) => {
assert.equal(data.state, 'passed');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
應該爲第一個斷言選擇正確的範圍——`approximately(actual, expected, range)`
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=unit&n=9').then(
(data) => {
assert.equal(data.assertions[0].method, 'approximately');
assert.equal(
data.assertions[0].args[2],
0.5,
"weirdNumbers(0.5) is in the range (0.5, 1.5]. It's within 1 +/- 0.5"
);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
應該爲第二個斷言選擇正確的範圍——`approximately(actual, expected, range)`
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=unit&n=9').then(
(data) => {
assert.equal(data.assertions[1].method, 'approximately');
assert.equal(
data.assertions[1].args[2],
0.8,
"weirdNumbers(0.2) is in the range (0.2, 1.2]. It's within 1 +/- 0.8"
);
},
(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.
*/
```