基本概念
suites
suites表示一個(gè)測(cè)試集,以函數(shù)describe封裝
describe
describe 是 Jasmine 的全局函數(shù),作為一個(gè) Test Suite 的開(kāi)始
它通常有 2 個(gè)參數(shù):字符串和方法
字符串 -> 特定 Suite 的名字和標(biāo)題
方法 -> 實(shí)現(xiàn) Suite 的代碼
describe("This is an exmaple suite", function() {
it("contains spec with an expectation", function() {
//測(cè)試true是否等于true
expect(true).toBe(true);
expect(false).toBe(false);
expect(false).not.toBe(true);
});
});
Specs
Specs 通過(guò)調(diào)用 it 的全局函數(shù)來(lái)定義
it和describe的參數(shù)相同
每個(gè) Spec 包含一個(gè)或多個(gè) expectations 來(lái)測(cè)試需要測(cè)試代碼
Expectations
Expectations 是由方法 expect 來(lái)定義
一值代表實(shí)際值,二值代表期望值
Matchers
Matcher實(shí)現(xiàn)了斷言的比較操作
可以在expect調(diào)用Matcher前加上not來(lái)實(shí)現(xiàn)一個(gè)否定的斷言(expect(a).not.toBe(false);)
常見(jiàn)的matchers:
toBe():相當(dāng)于===比較
toNotBe()
toBeDefined():檢查變量或?qū)傩允欠褚崖暶髑屹x值
toBeUndefined()
toBeNull():是否是null
toBeTruthy():如果轉(zhuǎn)換為布爾值,是否為true
toBeFalsy()
toBeLessThan():數(shù)值比較,小于
toBeGreaterThan():數(shù)值比較,大于
toEqual():相當(dāng)于==
注意與toBe()的區(qū)別:
一個(gè)新建的Object不是(not to be)另一個(gè)新建的Object,但是它們是相等(to equal)的
toNotEqual()
toContain():數(shù)組中是否包含元素(值)只能用于數(shù)組,不能用于對(duì)象
toBeCloseTo():數(shù)值比較時(shí)定義精度,先四舍五入后再比較
//true
it("The 'toBeCloseTo' matcher is for precision math comparison", function() {
var pi = 3.1415926,
e = 2.78;
expect(pi).not.toBeCloseTo(e, 2); //第一個(gè)參數(shù)為比較數(shù),第二個(gè)參數(shù)定義精度
expect(pi).toBeCloseTo(e, 0);
});
toHaveBeenCalled()
toHaveBeenCalledWith()
toMatch():按正則表達(dá)式匹配
toNotMatch()
toThrow():檢驗(yàn)一個(gè)函數(shù)是否會(huì)拋出一個(gè)錯(cuò)誤
Setup and Teardown
Jasmine 提供了全局的方法實(shí)現(xiàn)清理操作
在describe函數(shù)中,
beforeEach():每個(gè)Spec執(zhí)行之前執(zhí)行
afterEach(): 每個(gè)Spec執(zhí)行之后執(zhí)行。
beforeAll():所有的Specs執(zhí)行之前執(zhí)行,但只執(zhí)行一次
afterAll():所有的Specs執(zhí)行之后執(zhí)行,但只執(zhí)行一次
嵌套代碼塊
describe 可以嵌套, Specs 可以定義在任何一層
一個(gè) suite 可以由一組樹(shù)狀的方法組成
在每個(gè) spec 執(zhí)行前,Jasmine 遍歷樹(shù)結(jié)構(gòu),按順序執(zhí)行每個(gè) beforeEach 方法,Spec 執(zhí)行后,Jasmine 同樣執(zhí)行相應(yīng)的 afterEach
跳過(guò)測(cè)試代碼塊
Suites 和 Specs 分別可以用 xdescribe 和 xit 方法來(lái)禁用和掛起
被Disabled的Suites在執(zhí)行中會(huì)被跳過(guò),該Suite的結(jié)果也不會(huì)顯示在結(jié)果集中
被Pending的Spec不會(huì)被執(zhí)行,但是Spec的名字會(huì)在結(jié)果集中顯示,只是標(biāo)記為Pending
xdescribe("An example of xdescribe.", function() {
var gVar;
beforeEach(function() {
gVar = 3.6;
gVar += 1;
});
xit(" and xit", function() {
expect(gVar).toEqual(4.6);
});
});
一個(gè)沒(méi)有定義函數(shù)體的Sepc也會(huì)在結(jié)果集中被標(biāo)記為Pending
如果在Spec的函數(shù)體中調(diào)用pending()函數(shù),那么該Spec也會(huì)被標(biāo)記為Pending。pending()函數(shù)接受一個(gè)字符串參數(shù),該參數(shù)會(huì)在結(jié)果集中顯示在PENDING WITH MESSAGE:之后,作為為何被Pending的原因
describe("Pending specs", function() {
xit("can be declared 'xit'", function() {
expect(true).toBe(false);
});
it("can be declared with 'it' but without a function");
it("can be declared by calling 'pending' in the spec body", function() {
expect(true).toBe(false);
pending('this is why it is pending');
});
});
Spy
Spy能監(jiān)測(cè)任何function的調(diào)用和方法參數(shù)的調(diào)用痕跡。需使用2個(gè)特殊的Matcher:
- toHaveBeenCalled:可以檢查function是否被調(diào)用過(guò)
- toHaveBeenCalledWith: 可以檢查傳入?yún)?shù)是否被作為參數(shù)調(diào)用過(guò)
spyOn
使用 spyOn(obj,'function')
來(lái)為 obj 的 function 方法聲明一個(gè)Spy
對(duì)Spy函數(shù)的調(diào)用并不會(huì)影響真實(shí)的值
describe("A spy", function() {
var foo, bar = null;
beforeEach(function() {
foo = {
setBar: function(value) {
bar = value;
}
};
spyOn(foo, 'setBar');
foo.setBar(123);
foo.setBar(456, 'another param');
});
it("tracks that the spy was called", function() {
expect(foo.setBar).toHaveBeenCalled();
});
it("tracks all the arguments of its calls", function() {
expect(foo.setBar).toHaveBeenCalledWith(123);
expect(foo.setBar).toHaveBeenCalledWith(456, 'another param');
});
it("stops all execution on a function", function() {
// Spy的調(diào)用并不會(huì)影響真實(shí)的值,所以bar仍然是null
expect(bar).toBeNull();
});
});
and.callThrough
如果在spyOn之后鏈?zhǔn)秸{(diào)用and.callThrough,那么Spy除了跟蹤所有的函數(shù)調(diào)用外,還會(huì)直接調(diào)用函數(shù)額真實(shí)實(shí)現(xiàn),因此Spy返回的值就是函數(shù)調(diào)用后實(shí)際的值了
...
spyOn(foo, 'getBar').and.callThrough();
foo.setBar(123);
fetchedBar = foo.getBar();
it("tracks that the spy was called", function() {
expect(foo.getBar).toHaveBeenCalled();
});
it("should not effect other functions", function() {
expect(bar).toEqual(123);
});
it("when called returns the requested value", function() {
expect(fetchedBar).toEqual(123);
});
});
and.stub
在調(diào)用and.callThrough后,如果你想阻止spi繼續(xù)對(duì)實(shí)際值產(chǎn)生影響,你可以調(diào)用and.stub。也就是說(shuō),and.stub是將spi對(duì)實(shí)際實(shí)現(xiàn)的影響還原到最終的狀態(tài)——不影響實(shí)際值
spyOn(foo, 'setBar').and.callThrough();
foo.setBar(123);
// 實(shí)際的bar=123
expect(bar).toEqual(123);
// 調(diào)用and.stub()后,之后調(diào)用foo.setBar將不會(huì)影響bar的值。
foo.setBar.and.stub();
foo.setBar(456);
expect(bar).toBe(123);
bar = null;
foo.setBar(123);
expect(bar).toBe(null);
全局匹配謂詞
jasime.any
參數(shù)為一個(gè)構(gòu)造函數(shù),用于檢測(cè)該參數(shù)是否與實(shí)際值所對(duì)應(yīng)的構(gòu)造函數(shù)相匹配
describe("jasmine.any", function() {
it("matches any value", function() {
expect({}).toEqual(jasmine.any(Object));
expect(12).toEqual(jasmine.any(Number));
});
describe("when used with a spy", function() {
it("is useful for comparing arguments", function() {
var foo = jasmine.createSpy('foo');
foo(12, function() { return true; });
expect(foo).toHaveBeenCalledWith(jasmine.any(Number), jasmine.any(Function));
});
});
});
jasime.anything
用于檢測(cè)實(shí)際值是否為 null 或 undefined ,如果不為 null 或 undefined,則返回true
it("matches anything", function() {
expect(1).toEqual(jasmine.anything());
});
jasmine.objectContaining
用于檢測(cè)實(shí)際Object值中是否存在特定key/value對(duì)。
var foo;
beforeEach(function() {
foo = {
a: 1,
b: 2,
bar: "baz"
};
});
it("matches objects with the expect key/value pairs", function() {
expect(foo).toEqual(jasmine.objectContaining({ bar: "baz" }));
expect(foo).not.toEqual(jasmine.objectContaining({ c: 37 }));
});
jasmine.arrayContaining
用于檢測(cè)實(shí)際Array值中是否存在特定值。
this值
除了在describe函數(shù)開(kāi)始定義變量,用于各it函數(shù)共享數(shù)據(jù)外,還可以通過(guò)this關(guān)鍵字來(lái)共享數(shù)據(jù)。
在在每一個(gè)Spec的生命周期(beforeEach->it->afterEach)的開(kāi)始,都將有一個(gè)空的this對(duì)象(在開(kāi)始下一個(gè)Spec周期時(shí),this會(huì)被重置為空對(duì)象)。
參考目錄
JavaScript 單元測(cè)試框架:Jasmine 初探
JavaScript單元測(cè)試框架-Jasmine
web前端開(kāi)發(fā)七武器—Jasmine入門(mén)教程(上)