假如你正在使用 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()
在可以運(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)行測試文件:
$ 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)
既然測試全部通過,我們應(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)行測試,仍然可以全部通過。