如何用Mocha測試JavaScript-第2部分

昨天我們學習了Mocha的基礎知識。今天,我們會把Mocha整合到一個項目中,這樣就可以看到它是如何實際工作的。

如何用Mocha測試JavaScript-第2部分

這個教程要講什麼?

本教程將提供如何使用Mocha進行測試的小型真實示例。更多技術乾貨鎖定“朗妹兒”公眾號,每天都有更新哦~到教程結束時,我們將成功使用Mocha測試現有的JS文件。在讀本教程之前,應該先了解Mocha是什麼,如何分組測試以及如何使用斷言庫。如果需要複習,請參閱我的第一篇Mocha教程。

項目設置

上一個教程中我們已經安裝了Mocha。如果還沒有的話,請通過執行$ npm install -g mocha全局安裝好Mocha。

接下來,我們要創建一個名為temperature的項目目錄。在temperature目錄中,我們將創建一個app.js文件,以及一個名為 test 的文件夾。在test文件夾中,創建一個名為test.js的文件。最後,通過運行npm init初始化項目。在項目初始化期間,最重要的問題是 'test command:',回答 'mocha' 就可。這樣我們只需鍵入 npm test 就可以運行Mocha。

搞定後,應該有類似如下的文件結構:

1. temperature

2. |-- app.js

3. |-- package.json

4. |-- test

5. |-- test.js

package.json文件還應包含以下json:

1. "scripts": {

2. "test": "mocha"

3. },

有了上面所有東西后,我們就可以開始了!

我們在測試什麼?

我們將在本教程中測試我們的app.js文件。為了加快進程,我已經寫好了app.js的內容。繼續,將下面的代碼複製到app.js中:

1. cToF = function(celsius) {

2. if(!Number.isInteger(celsius)) return undefined;

3. return celsius * 9 / 5 + 32;

4. }

5.

6. fToC = function(fahrenheit) {

7. if(!Number.isInteger(fahrenheit)) return undefined;

8. return (fahrenheit - 32) * 5 / 9;

9. }

我們的app.js只有九行代碼,由兩個函數組成:

· cToF()- 是將攝氏溫度轉換為華氏溫度的函數。它有一個參數,即攝氏溫度,並返回對應的華氏溫度。

· fToC() - 是相反的函數。它將華氏溫度轉為攝氏wen d。它的一個參數是華氏溫度,並返回攝氏溫度。

· 這兩個函數最初都要檢測確保它們被傳遞進來的參數是一個整數。如果不是整數(字符串、空白、undefined、NaN等),則返回undefined,函數短路。

設置測試

我們將測試以確保這些函數正常運行。這是我們的test.js文件採用的結構:

1. 一個名為Temperature Conversion的測試組

2. 在該測試組中,還有其它兩個測試組,一個命名為cToF,一個命名為fToC

3. 每個測試組將有三個測試用例:兩個數字和一個非整數測試。

測試組

首先,我們用describe()設置兩個測試組:

1. var assert = require('assert');

2. describe('Temperature Conversion', function() {

3. describe('cToF', function() {

4. // tests here

5. });

6. describe('fToC', function() {

7. // tests here

8. });

9. });

上面,我們有兩個嵌套的測試組,兩個指定函數的describe塊在外部Temperature Conversion的describe塊內。現在有了基本的結構,我們就可以給每個內部測試組添加三個it()測試用例。

添加測試用例

三個測試用例都將使用內置的assert斷言庫。因為要測試相等性,所以我們要用assert.equal(actual,expected);

對於第一個測試,我們將測試一個應該保持不變的數字。如果你不知道的話,華氏溫度和攝氏溫度在零下40度是相等的。下面我們設置這個測試,以確保我們的函數正確處理數學:

1. it('should convert -40 celsius to -40 fahrenheit', function() {

2. assert.equal(-40, cToF(-40));

3. });

很棒!我們斷言,一旦我們通過函數cToF轉換數字-40,結果值也應等於-40。

對於下一個測試,我們要看一下水的凝固點。我們想確保0攝氏度等於32華氏度。

1. it('should convert 0 celsius to 32 fahrenheit', function() {

2. assert.equal(32, cToF(0));

3. });

一切都和第一個測試一樣,除了我們將數字0傳遞到函數中,並期望結果等於32。

對於最後一個測試,我們將測試一個空白字符串,並確保函數返回undefined:

1. it('should return undefined if no temperature is input', function(){

2. assert.equal(undefined, cToF(''));

3. });

對於這個測試,我們期望cToF(' ')的結果等於undefined。

真棒,我們所有的cToF測試都完成了!現在我們需要為函數fToC做同樣的事情。我不會帶你去寫這些測試 - 你現在應該能自己搞定。如下是它們應該看起來像的樣子:

1. it('should convert -40 fahrenheit to -40 celsius', function() {

2. assert.equal(-40, fToC(-40));

3. });

4. it('should convert 32 fahrenheit to 0 celsius', function() {

5. assert.equal(0, fToC(32));

6. });

7. it('should return undefined if no temperature is input', function(){

8. assert.equal(undefined, convert.fToC(''));

9. });

我們完成了...對吧?

真棒,我們的測試設置完了,現在只需用npm test運行即可:

1. 0 passing (20ms)

哦豁。發生了什麼事呢?

公開我們的函數

我們從未將我們的函數公開給Mocha。就是說test.js文件沒法與app.js文件中的函數進行交互。幸運的是,有很多簡單的方法可以做到這一點。

1. 我們要創建一個名為convert的空對象 let convert = {};

2. 我們要把每個函數變成新的convert對象上的一個方法,而不是兩個不同的函數。

3. 在app.js的末尾,我們要用module.exports公開convert對象。如果之前你從沒用過module.exports的話,它就是用來告訴JavaScript返回什麼對象作為require調用的結果。

下面我們研究一下代碼,看看如何用它:

1. //app.js

2.

3. let convert = {};

4.

5. convert.cToF = function(celsius) {

6. if(!Number.isInteger(celsius)) return undefined;

7. return celsius * 9 / 5 + 32;

8. }

9.

10. convert.fToC = function(fahrenheit) {

11. if(!Number.isInteger(fahrenheit)) return undefined;

12. return (fahrenheit - 32) * 5 / 9;

13. }

14.

15. module.exports = convert;

現在要做的只是在test.js中require我們的app.js文件,並且把測試中所用的函數名修改為包含convert對象。如下是最終的test.js代碼:

1. //test.js

2. let convert = require('../app.js')

3. let assert = require('assert');

4.

5. describe('Temperature Conversion', function() {

6. describe('cToF', function() {

7. it('should convert -40 celsius to -40 fahrenheit', function() {

8. assert.equal(-40, convert.cToF(-40));

9. });

10. it('should convert 0 celsius to 32 fahrenheit', function() {

11. assert.equal(32, convert.cToF(0));

12. });

13. it('should return undefined if no temperature is input', function() {

14. assert.equal(undefined, convert.cToF(''));

15. });

16. });

17. describe('fToC', function() {

18. it('should convert -40 fahrenheit to -40 celsius', function() {

19. assert.equal(-40, convert.fToC(-40));

20. });

21. it('should convert 32 fahrenheit to 0 celsius', function() {

22. assert.equal(0, convert.fToC(32));

23. });

24. it('should return undefined if no temperature is input', function() {

25. assert.equal(undefined, convert.fToC('a'));

26. });

27. });

28. });

運行測試

這次運行npm test時,一切都按預期工作了!

1. Temperature Conversion

2. cToF

3. √ should convert -40 celsius to -40 fahrenheit

4. √ should convert 0 celsius to 32 fahrenheit

5. √ should return undefined if no temperature is input

6. fToC

7. √ should convert -40 fahrenheit to -40 celsius

8. √ should convert 32 fahrenheit to 0 celsius

9. √ should return undefined if no temperature is input

10.

11. 6 passing (34ms)

搞定

乾的不錯!現在我們在一個單獨的文件中測試Mocha。還有更多的東西需要學習 - 去查看文檔吧,現在應該有了需要理解它們所需的所有工具了。


分享到:


相關文章: