鍍金池/ 教程/ HTML/ 使用 Nodeunit 測試
備忘錄模式
解釋器模式
類似 Python 的 zip 函數(shù)
類變量和實(shí)例變量
提示參數(shù)
指數(shù)對數(shù)運(yùn)算
檢查變量的類型是否為數(shù)組
由數(shù)組創(chuàng)建一個字符串
生成隨機(jī)數(shù)
刪除數(shù)組中的相同元素
大寫單詞首字母
雙向服務(wù)器
類的混合
計(jì)算復(fù)活節(jié)的日期
轉(zhuǎn)換弧度和度
找到上一個月(或下一個月)
雙向客戶端
橋接模式
嵌入 JavaScript
AJAX
觀察者模式
克隆對象(深度復(fù)制)
一個隨機(jī)整數(shù)函數(shù)
清理字符串前后的空白符
歸納數(shù)組
平方根倒數(shù)快速算法
適配器模式
打亂數(shù)組中的元素
將數(shù)組連接
使用數(shù)組來交換變量
更快的 Fibonacci 算法
服務(wù)器
服務(wù)端和客戶端的代碼重用
客戶端
查找子字符串
策略模式
CoffeeScrip 的 type 函數(shù)
由數(shù)組創(chuàng)建一個對象詞典
回調(diào)綁定
工廠方法模式
映射數(shù)組
當(dāng)函數(shù)括號不可選
生成可預(yù)測的隨機(jī)數(shù)
不使用 jQuery 的 Ajax 請求
把字符串轉(zhuǎn)換為小寫形式
類方法和實(shí)例方法
擴(kuò)展內(nèi)置對象
定義數(shù)組范圍
MongoDB
匹配字符串
創(chuàng)建一個不存在的對象字面值
列表推導(dǎo)
比較范圍
修飾模式
檢測每個元素
拆分字符串
字符串插值
對象數(shù)組
去抖動函數(shù)
使用 Nodeunit 測試
SQLite
單件模式
篩選數(shù)組
替換子字符串
數(shù)組最大值
計(jì)算(美國和加拿大的)感恩節(jié)日期
找到一個月中的最后一天
計(jì)算兩個日期中間的天數(shù)
基本的 HTTP 服務(wù)器
把字符串轉(zhuǎn)換為大寫形式
使用 HTML 命名實(shí)體替換 HTML 標(biāo)簽
For 循環(huán)
模板方法模式
重復(fù)字符串
使用 Jasmine 測試
對象的鏈?zhǔn)秸{(diào)用
數(shù)學(xué)常數(shù)
反轉(zhuǎn)數(shù)組
計(jì)算月球的相位
使用 Heregexes
查找子字符串
生成器模式
遞歸函數(shù)
HTTP 客戶端
創(chuàng)建 jQuery 插件
檢測與構(gòu)建丟失的函數(shù)
生成唯一ID
命令模式

使用 Nodeunit 測試

問題

假如你正在使用 CoffeeScript 并且想要驗(yàn)證功能是否與預(yù)期一致,便可以決定使用 Nodeunit 測試框架。

討論

Nodeunit 是一種 JavaScript 對于單元測試庫( Unit Testing libraries )中 xUnit 族的實(shí)現(xiàn),Java, Python, Ruby, Smalltalk 中均可以使用。

當(dāng)使用 xUnit 族測試框架時,你需要將所需測試的描述預(yù)期功能的代碼寫在一個文件中。

例如,我們希望我們的計(jì)算器可以進(jìn)行加法和減法,并且對于正負(fù)數(shù)均可以正確計(jì)算,我們的測試如下。

# test/calculator.test.coffee
Calculator = require '../calculator'
exports.CalculatorTest =
    'test can add two positive numbers': (test) ->
        calculator = new Calculator
        result = calculator.add 2, 3
        test.equal(result, 5)
        test.done()

    'test can handle negative number addition': (test) ->
        calculator = new Calculator
        result = calculator.add -10, 5
        test.equal(result,  -5)
        test.done()

    'test can subtract two positive numbers': (test) ->
        calculator = new Calculator
        result = calculator.subtract 10, 6
        test.equal(result, 4)
        test.done()

    'test can handle negative number subtraction': (test) ->
        calculator = new Calculator
        result = calculator.subtract 4, -6
        test.equal(result, 10)
        test.done()

安裝 Nodeunit

在可以運(yùn)行你的測試之前,你必須先安裝 Nodeunit :

首先創(chuàng)建一個 package.json 文件

{
  "name": "calculator",
  "version": "0.0.1",
  "scripts": {
    "test": "./node_modules/.bin/nodeunit test"
  },
  "dependencies": {
    "coffee-script": "~1.4.0",
    "nodeunit": "~0.7.4"
  }
}

接下來從一個終端運(yùn)行。

$ npm install

運(yùn)行測試

使用代碼行可以簡便地運(yùn)行測試文件:

$ npm test

測試失敗,因?yàn)槲覀儾]有 calculator.coffee

suki@Yuzuki:nodeunit_testing (master)$ npm test
npm WARN package.json calculator@0.0.1 No README.md file found!

> calculator@0.0.1 test /Users/suki/tmp/nodeunit_testing
> ./node_modules/.bin/nodeunit test

/Users/suki/tmp/nodeunit_testing/node_modules/nodeunit/lib/nodeunit.js:72
        if (err) throw err;
                       ^
Error: ENOENT, stat '/Users/suki/tmp/nodeunit_testing/test'
npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0

我們創(chuàng)建一個簡單文件

# calculator.coffee

class Calculator

module.exports = Calculator

并且重新運(yùn)行測試套件。

suki@Yuzuki:nodeunit_testing (master)$ npm test
npm WARN package.json calculator@0.0.1 No README.md file found!

> calculator@0.0.1 test /Users/suki/tmp/nodeunit_testing
> ./node_modules/.bin/nodeunit test

calculator.test
? CalculatorTest - test can add two positive numbers

TypeError: Object #<Calculator> has no method 'add'
  ...

? CalculatorTest - test can handle negative number addition

TypeError: Object #<Calculator> has no method 'add'
  ...

? CalculatorTest - test can subtract two positive numbers

TypeError: Object #<Calculator> has no method 'subtract'
  ...

? CalculatorTest - test can handle negative number subtraction

TypeError: Object #<Calculator> has no method 'subtract'
  ...

FAILURES: 4/4 assertions failed (31ms)
npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0

通過測試

讓我們對方法進(jìn)行實(shí)現(xiàn)來觀察測試是否可以通過。

# calculator.coffee

class Calculator

  add: (a, b) ->
    a + b

  subtract: (a, b) ->
    a - b

module.exports = Calculator

當(dāng)我們重新運(yùn)行測試時可以看到全部通過:

suki@Yuzuki:nodeunit_testing (master)$ npm test
npm WARN package.json calculator@0.0.1 No README.md file found!

> calculator@0.0.1 test /Users/suki/tmp/nodeunit_testing
> ./node_modules/.bin/nodeunit test

calculator.test
? CalculatorTest - test can add two positive numbers
? CalculatorTest - test can handle negative number addition
? CalculatorTest - test can subtract two positive numbers
? CalculatorTest - test can handle negative number subtraction

OK: 4 assertions (27ms)

重構(gòu)測試

既然測試全部通過,我們應(yīng)看一看我們的代碼或測試是否可以被重構(gòu)。

在我們的測試文件中,每個測試都創(chuàng)建了自己的 calculator 實(shí)例。這會使我們的測試相當(dāng)?shù)闹貜?fù),特別是對于大型的測試套件。理想情況下,我們應(yīng)該考慮將初始化代碼移動到每次測試之前運(yùn)行。

通常在其他的 xUnit 庫中,Nodeunit 會提供一個 setUp(以及 tearDown )功能會在測試前調(diào)用。

Calculator = require '../calculator'

exports.CalculatorTest =

    setUp: (callback) ->
        @calculator = new Calculator
        callback()

    'test can add two positive numbers': (test) ->
        result = @calculator.add 2, 3
        test.equal(result, 5)
        test.done()

    'test can handle negative number addition': (test) ->
        result = @calculator.add -10, 5
        test.equal(result,  -5)
        test.done()

    'test can subtract two positive numbers': (test) ->
        result = @calculator.subtract 10, 6
        test.equal(result, 4)
        test.done()

    'test can handle negative number subtraction': (test) ->
        result = @calculator.subtract 4, -6
        test.equal(result, 10)
        test.done()

我們可以重新運(yùn)行測試,仍然可以全部通過。

上一篇:篩選數(shù)組下一篇:解釋器模式