標籤:des style blog http io ar color os 使用
原文地址:http://www.moye.me/2014/11/22/bdd_mocha/
引子
今天造了個輪子:寫了個深拷貝任意JavaScript對象的模組(事實上npm上已經有類似的模組,純造輪子 :) 雖然是個輪子,依然是需要測試的,保不齊還有除我之外的人會用到。那麼問題來了,應該怎麼測試?
從需求視角來看,這樣的模組涉及到的資料類型繁多(Object/Array/Date/RegExp/Number/String/Function),每一種都需要有不同的測試案例,對於需求方來說,更關注的是模組在每個用例下啟動並執行表現——甚至可以說,這樣的模組是以需求為驅動的,即使一開始無法滿足所有用例,只要有明確的用例需求,就能夠迭代出正確的功能。
有沒有這樣的測試方法呢?答案是肯定的,那就是 BDD。
BDD
BDD(行為驅動開發 )是第二代的、由外及內的、基於拉(pull)的、多方利益相關者的(stakeholder)、多種可擴充的、高自動化的敏捷方法。它描述了一個互動迴圈,可以具有帶有良好定義的輸出(即工作中交付的結果):已測試過的軟體。
BDD作為理論方法學,強調以需求為導向,並鼓勵開發人員、QA與需求方之間的協作。BDD位於更宏觀的位置,讓開發人員“做正確的事”;而TDD注重細節,確保開發人員“正確的做事”。
BDD風格用例
describe(‘Array‘, function(){ before(function(){ // ... }); describe(‘#indexOf()‘, function(){ it(‘should return -1 when not present‘, function(){ [1,2,3].indexOf(4).should.equal(-1); }); }); after(function(){ // ... });});
代碼很易讀,有點DSL的意思。常用的BDD介面有:
describe():描述情境,在裡面可以設定Context,可包括多個測試案例,也可以嵌套情境
it():位於情境內,描述測試案例
before():所有測試案例的統一前置動作
after():所有測試案例的統一後置動作
beforeEach():每個測試案例的前置動作
afterEach():每個測試案例的後置動作
非同步支援
describe(‘File loading‘, function () { it(‘content should not empty‘, function (done) { require(‘fs‘).read(‘test.txt‘, function(err, res){ res.should.not.equal(null); done(); }); });});
done()為非同步方法呼叫完成回調,可用於下一用例的等待。
Mocha
Mocha 是一個優秀的JS測試架構,支援TDD/BDD,結合 should.js/expect/chai/better-assert,能輕鬆構建各種風格的測試案例。
此外,利用Mocha的reporter結合像istanbul 還能實現程式碼涵蓋範圍報表的匯出。
在Node項目中安裝Mocha,需先在package.json中指定測試目錄及包:
"scripts": { "test": "mocha --recursive --timeout 15000 -R spec --require should --harmony test/ --bail"},"dependencies": { "should": "*", "mocha": "*" }
然後使用npm install,隨後便能使用npm test跑測試案例了。
執行個體
假設我的輪子是這個樣子的:
module.exports.isObject = isObject;module.exports.isObject = isObject;module.exports.isString = isString;module.exports.isNumber = isNumber;module.exports.isFunction = isFunction;module.exports.isRegExp = isRegExp;module.exports.deepCopy = deepCopy;function getObjectType(obj) { return Object.prototype.toString.call(obj);}function isObject(obj) { return getObjectType(obj) === ‘[object Object]‘;}function isDate(obj) { return getObjectType(obj) === ‘[object Date]‘;}function isString(obj) { return getObjectType(obj) === ‘[object String]‘;}function isArray(obj) { return getObjectType(obj) === ‘[object Array]‘;}function isNumber(obj) { return getObjectType(obj) === ‘[object Number]‘;}function isFunction(obj) { return getObjectType(obj) === ‘[object Function]‘;}function isRegExp(obj){ return getObjectType(obj) === ‘[object RegExp]‘;}function deepCopy(obj) { var cloneObj = null; if (isArray(obj)) cloneObj = []; else if (isObject(obj)) cloneObj = {}; else if (isDate(obj)) cloneObj = new Date(obj.toJSON()); else if (isRegExp(obj)) cloneObj = new RegExp(obj.valueOf()); else cloneObj = obj; for (var key in obj) { if(obj.hasOwnProperty(key)) { var child = obj[key]; if (isObject(child) || isArray(child)) cloneObj[key] = deepCopy(child); else if (isNumber(child) || isString(child) || isFunction(child)) cloneObj[key] = child; } } return cloneObj;}
構造一個複雜的對象,來測測輪子到底行不行:
var copyUtils = require(‘node-deepcopy‘);var deepCopy = copyUtils.deepCopy, isFunction = copyUtils.isFunction, isRegExp = copyUtils.isRegExp;var toValue = function(obj){ return isFunction(obj) || isRegExp(obj) ? obj.toString() : JSON.stringify(obj);};describe(‘deep copy of the Complex Object‘, function () { var foo = function () { this.c = 3; this.a = [ {e: 2, f: ‘good‘, g: [1, 2, 3]} ]; this.b = ‘b‘; this.d = function () { console.log("I‘m foo.d.") }; }; it("duplication should equal to the source", function () { var h = new foo(); var cloned2 = deepCopy(h); toValue(cloned2).should.equal(toValue(h)); }); it("duplication should not equal to the source after modifying", function () { var h = new foo(); var cloned2 = deepCopy(h); cloned2.a[0].e = 5; cloned2.a[0].f = ‘666‘; toValue(cloned2).should.not.equal(toValue(h)); cloned2.a[0].g.splice(0, 2); toValue(h.a[0].g).should.not.equal(toValue(cloned2.a[0].g)); });});
npm test跑一下,可以看到測試通過:
如果在這一步遇到什麼問題,則需要根據情境和用例反向分析代碼,並進行修複。在現實情況中,這個順序也可能是倒過來的,先有BDD用例,然後才是匹配用例行為的代碼。
更多文章請移步我的blog新地址: http://www.moye.me/
[Node.js] 也說thisBDD和Mocha架構