Jasmine 是基於 BBD (behavior-driven development)測試框架javascript
行爲驅動測試的定義:它是經過用天然語言書寫非程序員可讀的測試用例擴展了測試驅動開發方法, 行爲驅動開發人員使用混合了領域中統一的語言的母語語言來描述他們的代碼的目的html
describe
函數包含兩個參數java
string
name 一個測試套件的名稱function
fn 實現測試套件的代碼塊it
函數定義兩個參數git
string
name spec的標題function
fn spec的函數expect
函數 接收一個 value
, 返回一個鏈式匹配對象
擁有如下匹配規則函數程序員
toBe
// 引用全等toEqual
// 深度遍歷,值全等toMatch
// 正則匹配, 支持字符串和正則表達式toContain
(字符串, 數組)toBeNull
// === nulltoBeTruthy
// Boolean(expect) === truetoBeLessThan
// expect < valuetoBeDefined
// expect !== undefinedtoBeUndefined
// expect === undefinedtoBeFalsy
// Boolean(expect) === falsetoBeGreaterThan
// expect > valuetoBeCloseTo
// value - precision <= expect < value + precisiontoThrow
// expect throw 'xx'toThrowError
expect throw type or match error messagefail
函數, 直接指定spec的錯誤緣由github
describe("A spec using the fail function", function() { var foo = function(x, callBack) { if (x) { callBack(); } }; it("should not call the callBack", function() { foo(false, function() { fail("Callback has been called"); }); }); });
一個suite 能夠有多個 describe
, 一個 describe
能夠有多個 it
, 一個it
能夠有多個 expect
正則表達式
任何匹配表達式均可以有not
前綴對象, 表示與期待的相反. 如數組
describe('a suite', function () { it('should not equal false if expect is true', function () { expect(true).not.toBe(false); }); });
在一個describe
中, 單元測試中的的 多個it
, 有共同的須要初始化的狀況。集中初始化和卸載框架
beforeEach
(describe
執行後,it
執行前調用)afterEach
(每個 it
執行後調用)beforeAll
(describe
執行後,it
執行前調用,只調用一次)afterAll
(全部 it
執行完成後調用)在beforeEach
, it
, afterEach
, 共享獨立的空this
對象異步
describe
塊beforeEach
, afterEach
只針對同級describe
函數做用域
describe("A spec", function() { var foo; beforeEach(function() { foo = 0; foo += 1; }); afterEach(function() { foo = 0; }); it("is just a function, so it can contain any code", function() { expect(foo).toEqual(1); }); it("can have more than one expectation", function() { expect(foo).toEqual(1); expect(true).toEqual(true); }); describe("nested inside a second describe", function() { var bar; beforeEach(function() { bar = 1; }); it("can reference both scopes as needed", function() { expect(foo).toEqual(bar); }); }); });
使用 xdescribe
函數 能夠跳過當前的測試套件
使用 xit
表示 該行爲測試待規範,跳過測試
spyOn
函數具備監控對象的函數是否調用的功能, spy化的對象函數, 容許有如下的匹配函數
toHaveBeenCalled
// 匹配是否調用toHaveBeenCalledTimes
// 匹配調用次數toHaveBeenCalledWith
// 匹配被調用的函數參數是否與預訂的一致spy化後的函數,能夠指定執行的返回值
var foo = { setBar: function(value) { bar = value; }, getBar: function() { return bar; } }; spyOn(foo, "getBar").and.returnValue(745); fetchedBar = foo.getBar(); // fetchBar is 745
spy化的函數
and.returnValue
(返回值)and.throwError
(拋出異常)and.callThrough
(直接返回調用結果)and.returnValues
(返回一個數組)and.callFake
(經過回調篡改返回值)and.stub
(凍結spy化的函數執行)and.identity
(返回一個name)spy 其它跟蹤屬性
calls.any()
(return true
表示被調用過, return false
表示從未調用過)calls.count()
(返回調用的次數)calls.argsFor(index)
(數組的形式返回某一次調用的入參)calls.allArgs()
(二維數組的形式返回全部調用的入參)calls.all()
(返回一個或多個{object: xx, args: [], returnValue: xx}的對象,多個由數組表示)calls.mostRecent()
(返回最近的一個調用上下文對象)calls.first()
(返回第一次調用的上下文對象)calls.reset()
(清空跟蹤數據)spyOn
是基於操做對象的函數調用進行tracking
, 而jasmine.createSpy
則是建立一個空的tracking
函數, jasmine.createSpyObj
能夠爲建立一個對象,擁有多個tracking
的函數
value
的函數jasmine.any
(匹配相同class
)jasmine.anything
(匹配 非 null
, undefined
的全部值)jasmine.objectContaining
(匹配對象局部的key
, value
)jasmine.arrayContaining
(匹配數組的某些項)jasmine.stringMatching
(匹配局部字符串)自定義匹配規則, 傳入一個對象, 對象中的 asymmetricMatch
是一個函數,其中入參表示姚培培的value
jasmine.clock().install
時間記錄啓動, jasmine.clock().tick(millsecond)
多少毫秒後,調用後續的expect
beforeAll
, afterAll
, beforeEach
, afterEach
, it
支持函數第一個參數 done
進行回調
describe("A spec using done.fail", function() { var foo = function(x, callBack1, callBack2) { if (x) { setTimeout(callBack1, 0); } else { setTimeout(callBack2, 0); } }; it("should not call the second callBack", function(done) { foo(true, done, function() { done.fail("Second callback has been called"); } ); }); });
done.fail
返回錯誤的執行