1.环境配置
bower是nodejs用于部署前端依赖的工具
利用bower设置基础环境
a. npm install -g bower
b. cd到目标文件夹
c. bower search jasmine
d. bower install jasmine (将安装最新版本的jasmine)
此时当前文件夹会生成
bower_components
jasmine是一套用于测试的js api,需要定义一个启动器,
编写report.html
<!DOCTYPE html>
<html>
<head>
<title>jasmine test</title>
<link rel="stylesheet" type="text/css" href="bower_components/jasmine/lib/jasmine-core/jasmine.css">
<script type="text/javascript" src="bower_components/jasmine/lib/jasmine-core/jasmine.js"></script>
<script type="text/javascript" src="bower_components/jasmine/lib/jasmine-core/jasmine-html.js"></script>
<script type="text/javascript" src="bower_components/jasmine/lib/jasmine-core/boot.js"></script>
</head>
<body>
<h1>jasmine test</h1>
<script type="text/javascript" src="src.js"></script>
<script type="text/javascript" src="test.js"></script>
</body>
</html>
jasmine.js 为核心api
jasmine.css 以及 jasmine-html.js用于生成report
boot.js 用于启动测试
src.js是自定义的被测试JS
test.js是测试用例JS
2. 测试
模板:
describe("suite name",function(){
beforeEach(function(){
//在每次用例或者子suite的用例被执行前调用
,依据父子顺序
});
afterEach(function(){
//在每次用例或者子suite的用例被执行后调用,依据父子顺序
});
it("case name",function(){
//case flow
.....
//expect
expect(xx).xxxx
})
xit("pending case",function(){
//让一个case处于待定状态
//也可以在正常case中使用pending()到达同样效果
});
describe("sub suite name",function(){
suite可以存在嵌套关系,用于组合用例
});
xdescribe("disabled suite name",function(){
//disabled
});
})
3.expect
expect(x).toBe(y) //x===y 类型数值全等
expect(x).not.toBe(y) //x!==y 类型数值全等
expect(x).toEqual(y) //x==y
expect(x).not.toEqual(y) //x!=y
expect(x).toMatch(regExp) //正则表达式匹配 eg:/a/.test('a')
expect(x).not.toMatch(regExp)
expect(x).toBeDefined(); // x!=undefined
expect(x).not.toBeDefined(); // x==undefined
expect(x).toBeUnDefined(); // x==undefined
expect(x).not.toBeUnDefined(); //
x!=undefined
expect(x).toBeNull(); // x!=null JS中null和undefined是不同的概念,undefined表示从未被声明过,而null是变量空值
expect(x).not.toBeNull(); //
x==null
expect(x).toBeTruthy(); //是否等价于true
expect(x).toBeFalsy(); //是否等价于false
//和js一样true false不单是boolean,其他的值也会与boolean等价,如1==true 'wqeqe'==true 0==false null等价于false
expect(x).not.toBeTruthy();
expect(x).not.toBeFalsy();
expect(x).toContain(y) //y元素是否在x数组内
expect(x).toBeLessThan(y) // x<y
expect(x).toBeGreaterThan(y) // x>y
expect(x).toBeCloseTo(e,n) //在小数精度n时,是否相等
expect(function).toThrow() //方法是否抛出异常
expect(function).toThrowError('error msg')
expect(function).toThrowError(TypeError,'xxx')
//检测方法是否会抛出指定的Error
expect(xx).toEqual(jasmine.any(Object))//x是任何的object,可以指定为其他数据类型 :Number Function ....
expect(x).toEqual(jasmin.objectContaining({a:'a'}))
//x是一个含有a属性,且a属性为'a'的对象
4.监听成员
obj={
a:function(a){console.log(a)}
}
spyOn(obj,'member')//监听obj对象的member成员,成员可以是方法
expect(obj.member).toHaveBeenCalled() //成员被调用
expect(obj.member).toHaveBeenCalledWith(args) //是否被指定参数调用
spyOn(obj,'member').and.returnValue(return)
//监听obj的member成员,并绑定其返回值为return
spyOn(obj,'member').and.callFake(function(){
return xxx;
})
//监听obj的member成员,被将其调用委托至指定的function
spyOn(obj,'member').and.throwError('error msg')
//监听obj的member成员,并强制抛出异常
expect(obj.member.calls.any()).toEqual(true) //.calls.any()将返回是否被调用过
//类似的.calls.count 调用次数 .calls.argsFor(i) 返回第i次调用参数
//.calls.allArgs 全部的调用参数 .calls.mostRecent 最近的调用参数
// .first 第一次 .reset重置calls记录
5. mock
mockFunction = jasmine.createSpy('mockFunction') //创建一个虚拟方法
mockFunction('xsaxs') //虚拟方法可以被正常调用 也如同spyOn
mockObj = jasmine.createSpyObj('mockObj',['member1','member2']) //创建一个虚拟对象,并指定他的成员,成员可以是function