在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

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

使用 Nodeunit 測(cè)試

問題

假如你正在使用 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ù)期功能的代碼寫在一個(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()

安裝 Nodeunit

在可以運(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"
  }
}

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

$ npm install

運(yùn)行測(cè)試

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

$ npm test

測(cè)試失敗,因?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)建一個(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

通過測(cè)試

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

# calculator.coffee

class Calculator

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

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

module.exports = Calculator

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

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)測(cè)試

既然測(cè)試全部通過,我們應(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è)試,仍然可以全部通過。

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