Jasmine - 文档(三)

简介: Jasmine - 文档(三)

前言

这是我翻译的Jasmine文档第三篇,前两篇文章的地址为:

Jasmine - 文档(一)

Jasmine - 文档(二)

匹配全局jasmine.any

jasmine.any传递一个构造函数(constructor)或者“类”(class)作为一个期望值,如果实际构造函数和传递的参数相同则返回true。

例如:

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));
    });
  });
});

匹配存在jasmine.anything

jasmine.anything函数只要实际值不为null或undefined则返回true。

例如:

describe("jasmine.anything", function() {
  it("matches anything", function() {
    expect(1).toEqual(jasmine.anything());
  });
  describe("when used with a spy", function() {
    it("is useful when the argument can be ignored", function() {
      var foo = jasmine.createSpy('foo');
      foo(12, function() {
        return false;
      });
      expect(foo).toHaveBeenCalledWith(12, jasmine.anything());
    });
  });
});

局部匹配jasmine.objectContaining

jasmine.objectContaining()用于匹配那些只关心实际值中是否存在预测键值对的时候,若存在则返回true。

例如:

describe("jasmine.objectContaining", function() {
  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
    }));
  });
  describe("when used with a spy", function() {
    it("is useful for comparing arguments", function() {
      var callback = jasmine.createSpy('callback');
      callback({
        bar: "baz"
      });
      expect(callback).toHaveBeenCalledWith(jasmine.objectContaining({
        bar: "baz"
      }));
      expect(callback).not.toHaveBeenCalledWith(jasmine.objectContaining({
        c: 37
      }));
    });
  });
});

局部数组匹配jasmine.arrayContaining

jasmine.arrayContaining()用于匹配实际数组中是否存在预测的值,存在则返回true。

例如:

describe("jasmine.arrayContaining", function() {
  var foo;
  beforeEach(function() {
    foo = [1, 2, 3, 4];
  });
  it("matches arrays with some of the values", function() {
    expect(foo).toEqual(jasmine.arrayContaining([3, 1]));
    expect(foo).not.toEqual(jasmine.arrayContaining([6]));
  });
  describe("when used with a spy", function() {
    it("is useful when comparing arguments", function() {
      var callback = jasmine.createSpy('callback');
      callback([1, 2, 3, 4]);
      expect(callback).toHaveBeenCalledWith(jasmine.arrayContaining([4, 2, 3]));
      expect(callback).not.toHaveBeenCalledWith(jasmine.arrayContaining([5, 2]));
    });
  });
});

字符串匹配jasmine.stringMatching

jasmine.stringMatching()用于你不想在一个较大的对象精确匹配字符串时,或者只是匹配spy预测值的部分字符串时,匹配成功返回true。

例如:

describe('jasmine.stringMatching', function() {
  it("matches as a regexp", function() {
    expect({foo: 'bar'}).toEqual({foo: jasmine.stringMatching(/^bar$/)});
    expect({foo: 'foobarbaz'}).toEqual({foo: jasmine.stringMatching('bar')});
  });
  describe("when used with a spy", function() {
    it("is useful for comparing arguments", function() {
      var callback = jasmine.createSpy('callback');
      callback('foobarbaz');
      expect(callback).toHaveBeenCalledWith(jasmine.stringMatching('bar'));
      expect(callback).not.toHaveBeenCalledWith(jasmine.stringMatching(/^bar$/));
    });
  });
});

定制不对称的测试器

当你需要检查符合一定标准但不必严格相等的一些东西时,通过调用asymmetricMatch方法可以简单地制定一个自定义的不对称测试器。

例如:

describe("custom asymmetry", function() {
  var tester = {
    asymmetricMatch: function(actual) {
      var secondValue = actual.split(',')[1];
      return secondValue === 'bar';
    }
  };
  it("dives in deep", function() {
    expect("foo,bar,baz,quux").toEqual(tester);
  });
  describe("when used with a spy", function() {
    it("is useful for comparing arguments", function() {
      var callback = jasmine.createSpy('callback');
      callback('foo,bar,baz');
      expect(callback).toHaveBeenCalledWith(tester);
    });
  });
});

Jasmine Clock

该语法在Jasmine2.0修改。Jasmine Clock用于测试与时间有关的代码。

在需要操作时间的spec或者Suites中调用jasmine.clock().install()来安装。而完成之后要确保卸载来回复原来的功能。

模拟JavaScript超时函数

你可以使用setTimeout或者setInterval超时同步执行已声明的函数。为了执行函数,使用jasmine.clock().tick()函数来推动时间前进,该函数传递一个毫秒数作为参数。

模拟日期

Jasmine Clock同样可以用来模拟当前日期。如果不提供一个基准时间给mockDate(),它将使用当前日期。

例如:

describe("Manually ticking the Jasmine Clock", function() {
  var timerCallback;
  beforeEach(function() {
    timerCallback = jasmine.createSpy("timerCallback");
    jasmine.clock().install();
  });
  afterEach(function() {
    jasmine.clock().uninstall();
  });
  it("causes a timeout to be called synchronously", function() {
    setTimeout(function() {
      timerCallback();
    }, 100);
    expect(timerCallback).not.toHaveBeenCalled();
    jasmine.clock().tick(101);
    expect(timerCallback).toHaveBeenCalled();
  });
  it("causes an interval to be called synchronously", function() {
    setInterval(function() {
      timerCallback();
    }, 100);
    expect(timerCallback).not.toHaveBeenCalled();
    jasmine.clock().tick(101);
    expect(timerCallback.calls.count()).toEqual(1);
    jasmine.clock().tick(50);
    expect(timerCallback.calls.count()).toEqual(1);
    jasmine.clock().tick(50);
    expect(timerCallback.calls.count()).toEqual(2);
  });
  describe("Mocking the Date object", function(){
    it("mocks the Date object and sets it to a given time", function() {
      var baseTime = new Date(2013, 9, 23);
      jasmine.clock().mockDate(baseTime);
      jasmine.clock().tick(50);
      expect(new Date().getTime()).toEqual(baseTime.getTime() + 50);
    });
  });
});

异步支持

该语法在Jasmine2.0开始支持。Jasmine同样支持执行包含异步操作。

调用beforeEachafterEachbeforeAllafterAllit时可以传递一个可选的单一变量,该变量在异步工作结束后可以调用。

Jasmine在判断超时失败前会默认等待5秒去完成异步操作。如果done还没执行,而5秒时间到期,当前的spec会被标记为失败,而接下来的会继续执行,就好像那个done已经调用了。

如果一个特定的spec需要更多时间,可以通过传递一个超时的值来调整。

如果所有的suite有一个不同的超时,jasmine.DEFAULT_TIMEOUT_INTERVAL可以在全局设置,在任何给定的describe之外。

describe("Asynchronous specs", function() {
  var value;
  beforeEach(function(done) {
    setTimeout(function() {
      value = 0;
      done();
    }, 1);
  });
  it("should support async execution of test preparation and expectations", function(done) {
//该片段在beforeEach里的done()调用之前不会开始执行
    value++;
    expect(value).toBeGreaterThan(0);
    done();//该片段在该done()没执行前并不会完成测试
  });
  describe("long asynchronous specs", function() {
    beforeEach(function(done) {
      done();
}, 1000);
    it("takes a long time", function(done) {
      setTimeout(function() {
        done();
      }, 9000);
    }, 10000);
    afterEach(function(done) {
      done();
    }, 1000);
  });
  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()可以导致spec失败,而标志该spec为完成
          done.fail("Second callback has been called");
        }
      );
    });
  });
});
目录
相关文章
|
5月前
|
数据采集 JavaScript 小程序
laravel8(四)使用simple_html_dom库解析html
首先:simple_html_dom下载地址: github.com/samacs/simp… 这是一个PHP解析html的一个库。 这玩意还是很有用的,比如我们在使用PHP做爬虫的时候,需要解析html中的内容等。 他的强大之处不仅仅只是帮助我们验证html文档;更能解析不符合W3C标准的html文档。 关于如何引入第三方类库,请移步《laravel5.8(十)引入第三方类库》laravel8中的设置方法也大同小异。 一:加载 html 有三种方式调用这个类: 1. 从url中加载html文档 2. 从字符串中加载html文档 3. 从文件中加载html文档
64 1
|
JavaScript 测试技术 API
|
3天前
|
JavaScript 测试技术
【sgGoogleTranslate】自定义组件:基于Vue.js用谷歌Google Translate翻译插件实现网站多国语言开发
【sgGoogleTranslate】自定义组件:基于Vue.js用谷歌Google Translate翻译插件实现网站多国语言开发
|
3天前
|
存储 开发工具 git
我为什么选择 Wiki.js 记笔记?
我为什么选择 Wiki.js 记笔记?
|
11月前
|
JavaScript 前端开发 Java
JavaScript —— JSchallenger Basics 基础练习(测试一下你的 JS 基础)【专题一】
JavaScript —— JSchallenger Basics 基础练习(测试一下你的 JS 基础)【专题一】
|
JavaScript 前端开发
vue中使用docx-preview插件预览word文档(后端express)
vue中使用docx-preview插件预览word文档(后端express)
773 0
|
JavaScript 前端开发 API
Bpmn.js 中文文档 之 Overlays
Bpmn.js 中文文档 之 Overlays
989 1
|
XML 前端开发 JavaScript
Bpmn.js 中文文档(一)
Bpmn.js 中文文档(一)
2819 0
|
前端开发 API
Bpmn.js 中文文档(二)
Bpmn.js 中文文档(二)
2040 0
|
SQL JavaScript 前端开发
使用 Vue.js 和 Semantic-UI 做一个简单的愿望清单
使用 Vue.js 和 Semantic-UI 做一个简单的愿望清单
182 0
使用 Vue.js 和 Semantic-UI 做一个简单的愿望清单