# 單元測試

有關測試部分

1.新增一個資料夾

2.terminal cd 進入該資料夾 ，執行`npm init`

3.安裝`mocha`與`should`兩個模組

> 注意:mocha要安裝在 global

```
npm install mocha -g
npm install mocha --save-dev
npm install should --save-dev
```

should的API可參考 <http://shouldjs.github.io/#assertion-above>

4.於package.json將script改為如下

```
"test": "./node_modules/.bin/mocha"
```

我們新增一個index.js

```
exports.test1 = (num) => {
  return 10 + num;
}
```

5.之後新增test資料夾

裡面放入test.js

```
var should = require('should');
var testFile = require('../index.js');
var assert = require('assert');

console.log(testFile.test1());

describe('10 + number', function(){
  it('should = 20', function(done){
    var total = testFile.test1(10);
    total.should.equal(20);
    done();
})
  it('should >= 20', function(done){
    var total = testFile.test1(20);
    total.should.be.above(20);
    done();
  })
})
```

然後在專案目錄輸入Mocha即可

他會自動去找專案內的test資料夾內的檔案執行


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://easonwang.gitbook.io/class/test.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
