Skip to content

Commit

Permalink
Automated testing with Mocha (#50)
Browse files Browse the repository at this point in the history
* feat: translation of Automated testing with Mocha

* Apply suggestions from code review

Co-Authored-By: Jason Huang <chaoju.huang@gmail.com>

* Apply suggestions from code review

Co-Authored-By: ArvinH <arvin0731@gmail.com>
  • Loading branch information
lenchen1112 and ArvinH authored Oct 30, 2019
1 parent cb57bf0 commit df109f5
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 139 deletions.
21 changes: 11 additions & 10 deletions 1-js/03-code-quality/05-testing-mocha/3-pow-test-wrong/solution.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
The test demonstrates one of the temptations a developer meets when writing tests.
該測試演示了開發者在寫測試時遇到的某種嘗試。

What we have here is actually 3 tests, but layed out as a single function with 3 asserts.
這邊我們實際上有三個測試,但卻只用單一個函式來寫三句斷言。

Sometimes it's easier to write this way, but if an error occurs, it's much less obvious what went wrong.
有時候這種方式更容易寫,但若問題產生時,想知道是什麼出問題也就更不明顯了。

If an error happens in the middle of a complex execution flow, then we'll have to figure out the data at that point. We'll actually have to *debug the test*.
若錯誤發生在複雜執行流程的中間,那我們得知道那個時間點的資料是什麼。我們實際上就必須 *除錯該測試*

It would be much better to break the test into multiple `it` blocks with clearly written inputs and outputs.
將測試打散為多個清楚寫下輸出輸入的 `it` 區塊會更好。

像這樣:

Like this:
```js
describe("Raises x to power n", function() {
it("5 in the power of 1 equals 5", function() {
Expand All @@ -25,10 +26,9 @@ describe("Raises x to power n", function() {
});
```

We replaced the single `it` with `describe` and a group of `it` blocks. Now if something fails we would see clearly what the data was.

Also we can isolate a single test and run it in standalone mode by writing `it.only` instead of `it`:
我們替換單一個 `it``describe` 並組合 `it` 區塊們。現在若有東西失敗了,我們將可以清楚看出資料是什麼。

同樣地,我們可以經由寫下 `it.only` 而非 `it`,以分離單一測試並在獨立模式中執行它:

```js
describe("Raises x to power n", function() {
Expand All @@ -37,7 +37,7 @@ describe("Raises x to power n", function() {
});

*!*
// Mocha will run only this block
// Mocha 將只運行這個區塊
it.only("5 in the power of 2 equals 25", function() {
assert.equal(pow(5, 2), 25);
});
Expand All @@ -48,3 +48,4 @@ describe("Raises x to power n", function() {
});
});
```

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# What's wrong in the test?
# 該測試中有什麼問題?

What's wrong in the test of `pow` below?
底下的 `pow` 測試有什麼問題?

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

P.S. Syntactically the test is correct and passes.
註:語法上來說,該測試是正確且可通過的。

Loading

0 comments on commit df109f5

Please sign in to comment.