jasmine官方api参考
jasmine
简介
jasmine 是一个行为驱动开发(TDD)测试框架, 一个js测试框架,它不依赖于浏览器、dom或其他js框架
jasmine有十分简介的语法
使用
从 这里 下载 stantd-alone安装包,并解压,双击打开里面的 specRunner.html, 即可看到测试示例,我们只要将自己的js源文件和测试文件分别放到 src 和 spec 文件夹中。
specRunner.html 到底是长什么样子的呢? f12 我们发现 先后加载了 jasmine.css, jasmine.js ,jasmine-html.js, boot.jsjasmine框架相关文件和 我们的 js源文件 jasmine测试文件
语法
分组 describe()
describe的作用的群组相关的测试, describe是可以嵌套的 从外到内 walkdown describe层级的 beforeEach, 由内到外walkup describe层级的 afterEach
describe('a suite', function(){ //这是一个测试分组
it('with an expactation',function(){
expect(true).toBe(true);
});
});
测试it()
describe 和 it 都是函数,所以它们可以包含任何js代码, 在 describe 中声明的变量和方法,能在 it中被访问。
it 代表的是具体的测试,当其中所有的断言都为true时,则该测试通过; 否则测试失败
describe('a suit is just a function', function(){
var a = 10;
it('and here is a test', function(){
var a = true;
expect(a).toBe(true);
});
});
期望expect()
desribe('the "toBe" matcher compares with "===" ', function(){
it('positive expect', function(){
expect(true).toBe(true);
});
it('negative expect', function(){
expect(false).not.toBe(true);
});
});
匹配 to*()
每个匹配方法在期望值和实际值之间执行逻辑比较,它负责告诉jasmine断言的真假,从而决定测试的成功或失败。
肯定断言 expect(true).toBe(true);
否定断言 expect(false).not.toBe(true);
jasmine有很丰富的匹配方法,而且可以自定义匹配方法。 内置的匹配方法有:
toBe()
toEqual()
toMatch()
toBeUndefined()
toBeNull()
toBeTruthy()
toContain()
toBeLessThan()
toBeCloseTo()
toThrowError()
describe("included matchers", function(){
it('"toBe" matcher compares width === ', function(){
var a = 12;
var b = a;
expect(a).toBe(b);
expect(a).not.toBe(null);
}); describe('"toEqual" matcher', function(){
it('work for simple literals and variable', function(){
var a = 12;
expect(a).toEqual(12);
}); it('should work for objects', function(){
var foo = {
a: 12,
b: 23
};
var bar = {
a: 12,
b: 23
}; expect(foo).toEqual(bar); //true?
});
}); it('"toMatch" matcher is for regular expression', function(){
var message = "foo bar baz";
expect(message).toMatch(/bar/);
expect(message).toMatch("bar");
expect(message).not.toMatch(/quue/);
}); it('"toBeUndefined" matcher compares against "undefined"', function(){
var a = {
foo: "foo"
}; expect(a.foo).not.toBeUndefined();
expect(a.bar).toBeUndefined();
}); it(' "toBeNull" matcher compares against "null"', function(){
var a = null;
var foo = 'foo'; expect(null).toBeNull();
expect(a).toBeNull();
expect(foo).not.toBeNull();
}); it('"toBeTruthy" matcher is for boolean casting testing' , function(){
var a, foo = 'foo';
expect(foo).toBeTruthy();
expect(a).not.toBeTruthy();
}); it('"toContain" matcher is for finding an item in an array', function(){
var a = ['foo', 'bar', 'baz'];
expect(a).toContain('bar');
expect(a).not.toContain('quu');
}); it('"toBeLessThan" matcher is for math comparisons', function(){
var n = 2.23, e = 1.33;
expect(e).toBeLessThan(n);
expect(n).not.toBeLessThan(e);
}); it('"toBeCloseTo" matcher is for precision match comparison', function(){
var n = 1.99, e = 2.35;
expect(e).not.toBeCloseTo(n, 2);
expect(e).toBeCloseTo(n, 0);
}); it('"toThrowError" matcher is for testing a specific thrown exception', function(){
var foo = function(){
throw new TypeError('foo bar baz');
};
expect(foo).toThrowError('foo bar baz');
expect(foo).toThrowError(/bar/);
expect(foo).toThrowError(TypeError);
expect(foo).toThrowError(TypeError, 'foo bar baz');
});
});
设置和清理 beforeEach(), afterEach()
beforeEach() 在它所属的 describe 块中的每条测试执行前,先执行的js代码, 作用就是设置和初始化一些东西。
afterEach() 和 beforeEach 相反,在 describe 块的每条测试执行后执行, 做一些清理的工作。
describe('tests with "setup" and "tear-down"', function(){
var foo;
beforeEach(function(){
foo = 0;
foo += 1; //每次测试前都初始化 foo == 1
});
afterEach(function(){
foo = 0; //每次测试完都重置 foo = 0;
});
it('it is just a function , so 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);
});
});
this对象
另一种在 beforeEach afterEach it 之间共享变量的方法: this对象, 每次执行完1条测试之后,this 都会被重置为空对象
describe('a suite', function(){
beforeEach(function(){
this.foo = 0;
});
it('can use "this" to share initial data', function(){
expect(this.foo).toEqual(0);
this.bar = "test pollution?";
});
it('prevents test pollution by having an empty "this" created for next test', function(){
expect(this.foo).toEqual(0);
expect(this.bar).toBe(undefined);
});
});
describe嵌套 beforeEach串行
describe('a suite', function(){
var foo;
beforeEach(function(){
foo = 0;
foo += 1;
});
afterEach(function(){
foo = 0;
});
it('a spec', function(){
expect(foo).toEqual(1);
});
it('a spec too', function(){
expect(foo).toEqual(1);
expect(true).toEqual(true);
});
describe('nested inside describe', function(){
var bar;
beforeEach(function(){
bar = 1;
});
// exec outer's describe beforeEach > this describe's beforeEach
it('可以访问外部describe的beforeEach的变量', function(){
expect(foo).toEqual(bar);
});
});
});
禁用describe或it
xdescribe(), xit() 和 pending()
xdescribe('a suite',function(){
//will not execute
});
describe('a suite too', function(){
xit('this test be canceled', function(){
expect(true).toBe(false);
});
it('can be desclared with "it" but without a function');
if('can be declared by calling "pending()" in spec body', function(){
expect(true).toBe(false);
pending(); //禁用该测试
});
});
函数调用监听 spy
spyOn() , toHaveBeenCalled() , toHaveBeenCalledWith()
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 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(); //setBar函数的执行 被spy监听器暂停了。
});
});
describe('a spy, when configured to call through', function(){
var foo , bar, fetchedBar;
beforeEach(function(){
foo = {
setBar: function(value){
bar = value;
},
getBar: function(){
return bar;
}
};
spyOn(foo, 'getBar').and.callThrough();
foo.setBar(123);
fetchedBar = foo.getBar();
});
it('tracks that the spy was called', function(){
expect(foo.getBar).toHaveBeenCalled();
});
it('should not effect other function', function(){
expect(bar).toEqual(123);
});
it('when called returns the requested value' , function(){
expect(fetchedBar).toEqual(123);
})
});
jasmine官方api参考的更多相关文章
- 关于基本类型值和引用类型值以及Vue官方API的array.$remove(reference)
今天又是孟哥解惑. 数组里的元素也是指向内存地址么? 这个要分情况的. 无论a[0],a[2]在什么地方,只要其值是基本类型值,就是值的比较,只要其值是引用类型(对象),就是内存地址的比较. Vue官 ...
- Google地图接口API之Google地图 API 参考手册(七)
Google 地图API 参考手册 地图 构造函数/对象 描述 Map() 在指定的 HTML 容器中创建新的地图,该容器通常是一个DIV元素. 叠加层 构造函数/对象 描述 Marker 创建一个标 ...
- Google Chart API 参考 中文版
Google Chart API 参考 中文版 文档信息 翻译: Cloudream ,最后修改:02/22/2008 06:11:08 英文版版权归 Google , 转载此中文版必须以链接形式注明 ...
- Zepto Api参考
zepto API参考 简介 Zepto是一个轻量级的针对现代高级浏览器的JavaScript库, 它与jquery有着类似的api. 如果你会用jquery,那么你也会用zepto. 设计目的 ze ...
- 国内值得关注的官方API集合
项目地址:https://github.com/marktony/Awesome_API 本页仅收集国内部分官方API,如需查看其他版本,请点击这里. 目录 笔记 出行 词典 电商 地图 电影 后端云 ...
- PJSUA2开发文档--第十二章 PJSUA2 API 参考手册
12 PJSUA2 API 参考手册 12.1 endpoint.hpp PJSUA2基本代理操作. namespace pj PJSUA2 API在pj命名空间内. 12.1.1 class En ...
- 通过官方API结合源码,如何分析程序流程
通过官方API结合源码,如何分析程序流程通过官方API找到我们关注的API的某个方法,然后把整个流程执行起来,然后在idea中,把我们关注的方法打上断点,然后通过Step Out,从内向外一层一层分析 ...
- Dubbo -- 系统学习 笔记 -- API参考手册
Dubbo -- 系统学习 笔记 -- 目录 API参考手册 配置API 注解API 模型API 上下文API 服务API API参考手册 Dubbo的常规功能,都保持零侵入,但有些功能不得不用API ...
- Springboot整合elasticSearch的官方API实例
前言:在上一篇博客中,我介绍了从零开始安装ElasticSearch,es是可以理解为一个操作数据的中间件,可以把它作为数据的存储仓库来对待,它具备强大的吞吐能力和计算能力,其基于Lucene服务器开 ...
随机推荐
- mysql安装常见问题(系统找不到指定的文件、发生系统错误 1067 进程意外终止)
在安装mysql时总是会遇到这样那样的问题,每次重新安装都会花很多时间来排查.在网上其实有很多相关的文章,但很多都只讲了方法,但没讲具体细节问题,导致无法解决问题.其实有时候知道问题的原因,但总是因为 ...
- BZOJ 3672: [Noi2014]购票( 树链剖分 + 线段树 + 凸包 )
s弄成前缀和(到根), dp(i) = min(dp(j) + (s(i)-s(j))*p(i)+q(i)). 链的情况大家都会做...就是用栈维护个下凸包, 插入时暴力弹栈, 查询时就在凸包上二分/ ...
- leetcode LRU Cache python
class Node(object): def __init__(self,k,x): self.key=k self.val=x self.prev=None self.next=None clas ...
- hdu 5726 GCD 倍增+ 二分
题目链接 给n个数, 定义一个运算f[l,r] = gcd(al, al+1,....ar). 然后给你m个询问, 每次询问给出l, r. 求出f[l, r]的值以及有多少对l', r' 使得f[l, ...
- 【算法】超大数组去重(Java语言实现)
要求时间复杂度和空间复杂度尽可能低! 情景一:需要返回的是不重复的数组. 情景二:只需要返回不重复的元素个数.
- c语言:从一组数据中选出可以组成三角形并且周长最长的三个数(简单)
题目如下: 思路分析: 写出完整的程序: /* 问题描述: 有n根棍子,棍子i的长度为ai.想要从中选出3根棍子组成周长尽可能长的三角形.请输 出最大的周长,若无法组成三角形则输出0. */ #inc ...
- scss + react + webpack + es6
scss + react + webpack + es6 写在前面: 刚学习完慕课网里的一个幻灯片案例,自己加了刚学的react,两者结合.首先让大家看看效果 点击此处 你可以先用纯js实现上面的效果 ...
- Matlab 之meshgrid, interp, griddata 用法和实例
http://blog.sina.com.cn/s/blog_67f37e760101bu4e.html 实例结果http://wenku.baidu.com/link?url=SiGsFZIxuS1 ...
- iOS6和iOS7代码的适配(3)——坐标适配
由于iOS7里面status bar和视图是重叠在一起了,所以应用的y坐标就没法和以前一致了,需要重新计算设定.基本上,你的应用用Xcode5运行一下就能看见这个问题,这里写了一个最简单的例子,一个V ...
- 假设写一个android桌面滑动切换屏幕的控件(一)
首先这个控件应该是继承ViewGroup: 初始化: public class MyGroup extends ViewGroup{ private Scroller mScroller; priva ...