W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
假如你正在使用CoffeeScript并且想要驗(yàn)證功能是否與預(yù)期一致,便可以決定使用Nodeunit測(cè)試框架。
Nodeunit是一種JavaScript對(duì)于單元測(cè)試庫(kù)( Unit Testing libraries )中xUnit族的實(shí)現(xiàn),Java, Python, Ruby, Smalltalk中均可以使用。
當(dāng)使用xUnit族測(cè)試框架時(shí),你需要將所需測(cè)試的描述預(yù)期功能的代碼寫(xiě)在一個(gè)文件中。
例如,我們希望我們的計(jì)算器可以進(jìn)行加法和減法,并且對(duì)于正負(fù)數(shù)均可以正確計(jì)算,我們的測(cè)試如下。
# 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)行你的測(cè)試之前,你必須先安裝Nodeunit:
首先創(chuàng)建一個(gè)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"
}
}
接下來(lái)從一個(gè)終端運(yùn)行。
$ npm install
使用代碼行可以簡(jiǎn)便地運(yùn)行測(cè)試文件:
$ npm test
測(cè)試失敗,因?yàn)槲覀儾](méi)有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)建一個(gè)簡(jiǎn)單文件
# calculator.coffee
class Calculator
module.exports = Calculator
并且重新運(yùn)行測(cè)試套件。
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
讓我們對(duì)方法進(jìn)行實(shí)現(xiàn)來(lái)觀察測(cè)試是否可以通過(guò)。
# calculator.coffee
class Calculator
add: (a, b) ->
a + b
subtract: (a, b) ->
a - b
module.exports = Calculator
當(dāng)我們重新運(yùn)行測(cè)試時(shí)可以看到全部通過(guò):
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)
既然測(cè)試全部通過(guò),我們應(yīng)看一看我們的代碼或測(cè)試是否可以被重構(gòu)。
在我們的測(cè)試文件中,每個(gè)測(cè)試都創(chuàng)建了自己的calculator實(shí)例。這會(huì)使我們的測(cè)試相當(dāng)?shù)闹貜?fù),特別是對(duì)于大型的測(cè)試套件。理想情況下,我們應(yīng)該考慮將初始化代碼移動(dòng)到每次測(cè)試之前運(yùn)行。
通常在其他的xUnit庫(kù)中,Nodeunit會(huì)提供一個(gè)setUp(以及tearDown)功能會(huì)在測(cè)試前調(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)行測(cè)試,仍然可以全部通過(guò)。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: