Javascript测试框架Jasmine(二):Matchers

来源:互联网 发布:淘宝真正的原单店 编辑:程序博客网 时间:2024/06/16 07:37

zz from: http://keenwon.com/1197.html


上一篇稍微介绍了一下,这一篇讲讲Matcher。在Jasmine中,每个Matcher实现一个“期望值”和“实际值”的布尔判断,Jasmine会根据Mather判断expectation是true 还是false ,然后决定spec是测试通过还是失败。所有Matcher可以通过not 执行否定判断。例如:

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

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

toBe

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

  1. it("toBe Matcher用来执行===对比", function() {
  2. var a = 12;
  3. var b = a;
  4.  
  5. expect(a).toBe(b);
  6. expect(a).not.toBe(null);
  7. });

在线执行

toEqual

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

  1. describe("toEqual判断两个对象是否相等:", function() {
  2.  
  3. it("对比简单的值类型的变量", function() {
  4. var a = 12;
  5. expect(a).toEqual(12);
  6. });
  7.  
  8. it("对比对象", function() {
  9. var foo = {
  10. a: 12,
  11. b: 34
  12. };
  13. var bar = {
  14. a: 12,
  15. b: 34
  16. };
  17. expect(foo).toEqual(bar);
  18. });
  19. });

在线执行

toMatch

使用正则匹配:

  1. it("toMatch用来进行正则匹配", function() {
  2. var message = "foo bar baz";
  3.  
  4. expect(message).toMatch(/bar/);
  5. expect(message).toMatch("bar");
  6. expect(message).not.toMatch(/quux/);
  7. });

在线执行

toBeDefined

判断是否undefined:

  1. it("toBeDefined判断是否非undefined", function() {
  2. var a = {
  3. foo: "foo"
  4. };
  5.  
  6. expect(a.foo).toBeDefined();
  7. expect(a.bar).not.toBeDefined();
  8. });

在线执行

toBeUndefined

判断是否是undefined:

  1. it("toBeUndefined判断是否是undefined", function() {
  2. var a = {
  3. foo: "foo"
  4. };
  5.  
  6. expect(a.foo).not.toBeUndefined();
  7. expect(a.bar).toBeUndefined();
  8. });

在线执行

toBeNull

判断是否为null:

  1. it("toBeNull用来判断是否为null", function() {
  2. var a = null;
  3. var foo = "foo";
  4.  
  5. expect(null).toBeNull();
  6. expect(a).toBeNull();
  7. expect(foo).not.toBeNull();
  8. });

在线执行

toBeTruthy

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

  1. it("toBeTruthy执行布尔测试,判断值是否是,或者可以转换为true", function() {
  2. var a, foo = "foo";
  3.  
  4. expect(foo).toBeTruthy();
  5. expect(a).not.toBeTruthy();
  6. });

在线执行

toBeFalsy

布尔测试,于toBeTruthy相反:

  1. it("toBeFalsy和toBeTruthy相反", function() {
  2. var a, foo = "foo";
  3.  
  4. expect(a).toBeFalsy();
  5. expect(foo).not.toBeFalsy();
  6. });

在线执行

toContain

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

  1. it("toContain判断一个数组是否包含某个值", function() {
  2. var a = ["foo", "bar", "baz"];
  3.  
  4. expect(a).toContain("bar");
  5. expect(a).not.toContain("quux");
  6. });

在线执行

toBeLessThan

数字大小比较:

  1. it("toBeLessThan执行数字大小比较", function() {
  2. var pi = 3.1415926,
  3. e = 2.78;
  4.  
  5. expect(e).toBeLessThan(pi);
  6. expect(pi).not.toBeLessThan(e);
  7. });

在线执行

toBeGreaterThan

和toBeLessThan相反:

  1. it("toBeGreaterThan和toBeLessThan相反", function() {
  2. var pi = 3.1415926,
  3. e = 2.78;
  4.  
  5. expect(pi).toBeGreaterThan(e);
  6. expect(e).not.toBeGreaterThan(pi);
  7. });

在线执行

toBeCloseTo

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

  1. it("toBeCloseTo示例", function() {
  2. var pi = 3.1415926,
  3. e = 2.78;
  4.  
  5. expect(pi).not.toBeCloseTo(e, 2);
  6. expect(pi).toBeCloseTo(e, 0);
  7. });

再看看它的源码:

  1. getJasmineRequireObj().toBeCloseTo = function () {
  2.  
  3. function toBeCloseTo() {
  4. return {
  5. compare: function (actual, expected, precision) {
  6. if (precision !== 0) {
  7. precision = precision || 2;
  8. }
  9.  
  10. return {
  11. pass: Math.abs(expected - actual) < (Math.pow(10, -precision) / 2)
  12. };
  13. }
  14. };
  15. }
  16.  
  17. return toBeCloseTo;
  18. };

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

在线执行

toThrow

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

  1. it("toThrow判断一个函数是否有抛出异常", function() {
  2. var foo = function() {
  3. return 1 + 2;
  4. };
  5. var bar = function() {
  6. return a + 1; //a不存在
  7. };
  8.  
  9. expect(foo).not.toThrow();
  10. expect(bar).toThrow();
  11. });

在线执行


原创粉丝点击