1. 什么是Jasmine
Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests.This guide is running against Jasmine version 2.4.1.
Jasmine github主页
官网教程: 英文,简单明了,自带demo,强烈推荐学习
Jasmine下载: 里面有一个自带的例子,SpecRunner.html, 打开就知道jasmine的大致使用了。
2. 基本使用
跟Qunit差不错,首先要有一个模板接收测试结果。
当然也可以配合自动化工具,例如grunt,直接显示在控制台。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Jasmine v2.4.1</title> <!-- jasmine运行需要的文件 --> <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.4.1/jasmine_favicon.png"> <link rel="stylesheet" href="lib/jasmine-2.4.1/jasmine.css"> <script src="lib/jasmine-2.4.1/jasmine.js"></script> <script src="lib/jasmine-2.4.1/jasmine-html.js"></script> <script src="lib/jasmine-2.4.1/boot.js"></script> <!-- 要测试的代码 --> <script src="src/SourceForTest.js"></script> <!-- 测试用例 --> <script src="spec/TestCases.js"></script> </head> <body> </body> </html> |
测试用例如下
describe("A suite", function() {
it("contains spec with an expectation", function() {
expect(true).toBe(true);
expect(false).not.toBe(true);
//fail("test will fail if this code excute"); //可用于不想被执行的函数中
});
});
describe定义一组测试,可嵌套;
it定义其中一个测试;
注意:应该按BDD的模式来写describe与it的描述。
关于BDD可参考:说起BDD,你会想到什么?
expect()的参数是需要测试的东西,toBe()是一种断言,相当于===,not相当于取非。
jasmine还有更多的断言,并且支持自定义断言,详细可见官网教程。
3. SetUp与TearDown
在jasmine中用beforeEach,afterEach,beforeAll,afterAll来表示
describe("A spec using beforeEach and afterEach", function() { var foo = 0; beforeEach(function() { foo += 1; }); afterEach(function() { foo = 0; }); it("is just a function, so it can contain any code", function() { expect(foo).toEqual(1); }); it("can have more than one expectation", function() { expect(foo).toEqual(1); expect(true).toEqual(true); }); }); |
4. 自定义断言(matcher)
官网custom_matcher教程
beforeEach(function(){ jasmine.addMatchers({ toBeSomeThing: function(){ //定义断言的名字 return { compare: function (actual, expected) { //compare是必须的 var foo = actual; return { pass: foo === expected || 'something' , message: "error message here" //断言为false时的信息 } //要返回一个带pass属性的对象,pass就是需要返回的布尔值 //negativeCompare: function(actual, expected){ ... } //自定义not.的用法 } }; } }); }); |
5. this的用法
每进行一个it测试前,this都会指向一个空的对象。可以利用这一点来共享变量,而不用担心变量被修改
describe("A spec", function() { beforeEach(function() { this.foo = 0; }); it("can use the `this` to share state", function() { expect(this.foo).toEqual(0); this.bar = "test pollution?"; }); it("prevents test pollution by having an empty `this` created for the next spec", function() { expect(this.foo).toEqual(0); expect(this.bar).toBe(undefined); //true }); }); |
6. 闲置测试
在describe和it前加一个x,变成xdescribe,xit,就可以闲置该测试,这样运行时就不会自动测试,需要手动开始。
7. Spy
功能强大的函数监听器,可以监听函数的调用次数,传参等等,甚至可以伪造返回值,详细可参考官网教程
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 that the spy was called x times", function() { expect(foo.setBar).toHaveBeenCalledTimes(2); }); 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() { expect(bar).toBeNull(); }); }); |