您的位置:首页 > Web前端 > JavaScript

Javascript测试框架Jasmine(二):Matchers

2017-11-09 22:06 766 查看
zz from: http://keenwon.com/1197.html
上一篇稍微介绍了一下,这一篇讲讲Matcher。在Jasmine中,每个Matcher实现一个“期望值”和“实际值”的布尔判断,Jasmine会根据Mather判断expectation是
true
还是
false
,然后决定spec是测试通过还是失败。所有Matcher可以通过
not
执行否定判断。例如:

it("not示例", function() {
expect(false).not.toBe(true);
});


Jasmine提供了丰富的内置Matchers,下面简单讲一下:


toBe


toBe Matcher用来执行
===
对比:

it("toBe Matcher用来执行===对比", function() {
var a = 12;
var b = a;

expect(a).toBe(b);
expect(a).not.toBe(null);
});


在线执行


toEqual


判断两个对象是否相等,可以对比简单的值类型的变量和对象:

describe("toEqual判断两个对象是否相等:", function() {

it("对比简单的值类型的变量", function() {
var a = 12;
expect(a).toEqual(12);
});

it("对比对象", function() {
var foo = {
a: 12,
b: 34
};
var bar = {
a: 12,
b: 34
};
expect(foo).toEqual(bar);
});
});


在线执行


toMatch


使用正则匹配:

it("toMatch用来进行正则匹配", function() {
var message = "foo bar baz";

expect(message).toMatch(/bar/);
expect(message).toMatch("bar");
expect(message).not.toMatch(/quux/);
});


在线执行


toBeDefined


判断是否非undefined:

it("toBeDefined判断是否非undefined", function() {
var a = {
foo: "foo"
};

expect(a.foo).toBeDefined();
expect(a.bar).not.toBeDefined();
});


在线执行


toBeUndefined


判断是否是undefined:

it("toBeUndefined判断是否是undefined", function() {
var a = {
foo: "foo"
};

expect(a.foo).not.toBeUndefined();
expect(a.bar).toBeUndefined();
});


在线执行


toBeNull


判断是否为null:

it("toBeNull用来判断是否为null", function() {
var a = null;
var foo = "foo";

expect(null).toBeNull();
expect(a).toBeNull();
expect(foo).not.toBeNull();
});


在线执行


toBeTruthy


布尔测试,判断值是否是,或者可以转换为true:

it("toBeTruthy执行布尔测试,判断值是否是,或者可以转换为true", function() {
var a, foo = "foo";

expect(foo).toBeTruthy();
expect(a).not.toBeTruthy();
});


在线执行


toBeFalsy


布尔测试,于toBeTruthy相反:

it("toBeFalsy和toBeTruthy相反", function() {
var a, foo = "foo";

expect(a).toBeFalsy();
expect(foo).not.toBeFalsy();
});


在线执行


toContain


判断一个数组是否包含某个值:

it("toContain判断一个数组是否包含某个值", function() {
var a = ["foo", "bar", "baz"];

expect(a).toContain("bar");
expect(a).not.toContain("quux");
});


在线执行


toBeLessThan


数字大小比较:

it("toBeLessThan执行数字大小比较", function() {
var pi = 3.1415926,
e = 2.78;

expect(e).toBeLessThan(pi);
expect(pi).not.toBeLessThan(e);
});


在线执行


toBeGreaterThan


和toBeLessThan相反:

it("toBeGreaterThan和toBeLessThan相反", function() {
var pi = 3.1415926,
e = 2.78;

expect(pi).toBeGreaterThan(e);
expect(e).not.toBeGreaterThan(pi);
});


在线执行


toBeCloseTo


前面都作用一目了然,这个就有一点点不还理解了,还是想看例子:

it("toBeCloseTo示例", function() {
var pi = 3.1415926,
e = 2.78;

expect(pi).not.toBeCloseTo(e, 2);
expect(pi).toBeCloseTo(e, 0);
});


再看看它的源码:

getJasmineRequireObj().toBeCloseTo = function () {

function toBeCloseTo() {
return {
compare: function (actual, expected, precision) {
if (precision !== 0) {
precision = precision || 2;
}

return {
pass: Math.abs(expected - actual) < (Math.pow(10, -precision) / 2)
};
}
};
}

return toBeCloseTo;
};


这下就比较清晰了,
toBeCloseTo
是比较两个值是否足够接近(不一定要相等),而这个“足够接近”就是
toBeCloseTo
的第二个参数指定的,它表示小数点后位数除以2(
Math.pow(10,
-precision) / 2
)。

在线执行


toThrow


判断一个函数是否有抛出异常:

it("toThrow判断一个函数是否有抛出异常", function() {
var foo = function() {
return 1 + 2;
};
var bar = function() {
return a + 1; //a不存在
};

expect(foo).not.toThrow();
expect(bar).toThrow();
});


在线执行
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: