Suites(describe)是Jasmine的核心,裏面包括多個specs測試體(it),而每一個測試體specs裏面可能包含多個斷言(expect);html
describe()
測試集/*@param * string 用於描述測試組的名稱 * function 是測試組的主體函數 */ describe("測試集描述", function () { // 測試集主體函數 })
it()
測試體,包含在describe()
,傳入兩個參數/*@param * string 用於描述測試體的名稱 * function 是測試體的主體函數 */ describe("測試集描述", function () { it("測試體描述",function(){ // 測試體主體函數 }) })
beforeEach()
在describe函數中每一個Spec(it)執行以前執行。git
afterEach()
在describe函數中每一個Spec(it)數執行以後執行。github
beforeAll()
在describe函數中全部的Spec(it)執行以前執行,但只執行一次,在Sepc之間並不會被執行。正則表達式
afterAll()
在describe函數中全部的Spec(it)執行以後執行,但只執行一次,在Sepc之間並不會被執行。api
describe("測試集描述", function () { beforeEach(function(){ // 每一個it執行前執行函數體 }) })
expect()
斷言,傳遞一個參數,接收實際值,後面緊跟着一個Matchers(),來進行判斷是否經過describe("測試集描述", function () { it("測試體描述",function(){ var a=true; expect(a).toEqual(true); }) })
Matchers()
常見的有:not
表示對下面斷言的否認數組
toBe()
判斷兩個變量是否全等,相似於「===」;框架
toNotBe()
與上一個相反,判斷兩個變量是否不全等,相似於「!==」;函數
toEqual()
判斷變量是否相等,至關於「==」;單元測試
toThrow()
檢驗一個函數是否會拋出一個錯誤。測試
toBeDefined()
檢查變量或屬性是否已聲明且賦值(是否不等於undefined);
toBeUndefined()
檢查變量或屬性是否已聲明且賦值(是否等於undefined);
toBeNull()
判斷變量是否爲null;
toBeTruthy()
判斷變量若是轉換爲布爾值,是否爲true;
toBeFalsy()
判斷變量若是轉換爲布爾值,是否爲false;
toBeLessThan()
與數值比較,是否小於;
toBeGreaterThan()
與數值比較,是否大於;
toContain()
判斷一個數組中是否包含元素(值)。只能用於數組,不能用於對象;
toBeCloseTo()
數值比較時定義精度,先四捨五入後再比較;
toMatch()
按正則表達式匹配;
toNotMatch()
不按正則表達式匹配;
describe("測試集描述", function () { it("測試體描述",function(){ var a=true; expect(a).not.toEqual(false); }) })
在
describe
和it
前加一個x,變成xdescribe
,xit
,就能夠閒置該測試,這樣運行時就不會自動測試,須要手動開始。
xdescribe("測試集描述", function () { xit("測試體描述",function(){ }) })
jasmine.addMatchers({ mySelfMatcher: function () { //定義斷言的名字 return { compare: function (actual, expected) { //compare是必須的 return { pass: actual === expected, message: "song is not current playing" //斷言爲false時的信息 } //要返回一個帶pass屬性的對象,pass就是須要返回的布爾值 } //negativeCompare: function(actual, expected){ ... } //自定義not.的用法 }; } });
調用
... // actual 爲要測試的值,expect指望值 expect(actual).mySelfMatcher(expect);
能監測任何
function
的調用和方法參數的調用痕跡,可是不會影響函數真實返回值,需使用2個特殊的Matcher
toHaveBeenCalled
能夠檢查function是否被調用過
toHaveBeenCalledWith
能夠檢查傳入參數是否被做爲參數調用過
describe("spy", function() { var foo, bar = null; beforeEach(function() { foo = { setBar: function(value) { bar = value; } }; spyOn(foo, 'setBar'); foo.setBar(123); rue foo.setBar(456, 'another param'); }); it("跟蹤函數調用狀況", function() { // 上面調用了foo.setBar,因此返回true expect(foo.setBar).toHaveBeenCalled(); // 上面調用了foo.setBar,而且傳入參數123,因此返回true expect(foo.setBar).toHaveBeenCalledWith(456, 'another param'); // Spy的調用並不會影響真實的值,因此bar仍然是null。 expect(bar).toBeNull(); }); });
and.callThrough
若是在spyOn以後鏈式調用and.callThrough
,那麼Spy除了跟蹤全部的函數調用外,而且Spy返回的值是函數調用後實際的值。
describe("spy", function() { ... spyOn(foo, 'setBar').and.callThrough(); foo.setBar(123); it("跟蹤函數調用狀況", function() { // Spy後面添加了and.callThrough(),返回的值是函數調用後實際的值,因此此時bar變爲了123,而不是null。 expect(bar).toEqual(123); }); });
and.stub
在調用and.callThrough()
後,能夠阻止spy對實際值的影響,也就是將and.callThrough()
方法執行之後進行還原
describe("spy", function() { ... it("跟蹤函數調用狀況", function() { spyOn(foo, 'setBar').and.callThrough(); foo.setBar(123); expect(bar).toEqual(123); // 調用and.callThrough(),影響了bar的值 bar = null; foo.setBar.and.stub();// 調用and.stub()後,以後調用foo.setBar將不會影響bar的值。 foo.setBar(123); expect(bar).toBeNull();// 沒有影響bar的值,bar此時爲null }); });