Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
这些测试代码演示了开发人员在编写测试代码时遇到的一些疑惑
这些测试代码展示了开发人员在编写测试代码时遇到的一些疑惑

我们这里实际上有三条测试,但是用了一个函数来放置 3 个断言语句。

有时用这种方式编写会更容易,但是如果发生错误,那么出错的情况就不那么明显了
有时用这种方式编写会更容易,但是如果发生错误,那么到底什么出错了就很不明显

如果在复杂的执行流程中发生了一个错误,那么我们必须在那找出数据。 我们实际上必须**调试测试**。
如果错误发生在一个复杂的执行流的中间,那么我们就必须找出那个点的数据。我们必须 **调试测试**。

将测试分成多个具有明确输入和输出的 `it` 代码块中会更好
将测试分成多个具有明确输入和输出的 `it` 代码块会更好

像是这样:
```js
Expand All @@ -25,9 +25,9 @@ describe("Raises x to power n", function() {
});
```

我们使用 `describe` 和一组 `it` 代码块替换掉了单个的 `it`。现在,如果某个测试失败了,我们会清楚地看到数据是什么
我们使用 `describe` 和一组 `it` 代码块替换掉了单个的 `it`。现在,如果某个测试失败了,我们可以清楚地看到数据是什么

此外,我们可以通过编写`it.only` 而不是 `it` 来隔离单个测试并以独立模式运行它
此外,我们可以通过编写 `it.only` 而不是 `it` 来隔离单个测试,并以独立模式运行它


```js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ importance: 5

# 测试代码中有什么错误?

下面测试代码中的 `pow` 有什么错误
下面这个 `pow` 的测试代码有什么错误

```js
it("Raises x to the power n", function() {
Expand All @@ -21,4 +21,4 @@ it("Raises x to the power n", function() {
});
```

附:从语法上来说这些测试代码是正确且能通过的
附:从语法上来说这些测试代码是正确且通过的
175 changes: 86 additions & 89 deletions 1-js/03-code-quality/05-testing-mocha/article.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<body>

<script>
function pow() {
function pow(x, n) {
return 8; // :) we cheat!
}
</script>
Expand Down
2 changes: 1 addition & 1 deletion 1-js/03-code-quality/05-testing-mocha/pow-4.view/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe("pow", function() {

describe("raises x to power n", function() {
describe("raises x to power 3", function() {

function makeTest(x) {
let expected = x * x * x;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe("pow", function() {

describe("raises x to power n", function() {
describe("raises x to power 3", function() {

function makeTest(x) {
let expected = x * x * x;
Expand Down
2 changes: 1 addition & 1 deletion 1-js/03-code-quality/05-testing-mocha/pow-nan.view/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe("pow", function() {

describe("raises x to power n", function() {
describe("raises x to power 3", function() {

function makeTest(x) {
let expected = x * x * x;
Expand Down