jasmine中的matchers
2015-12-11 18:22
190 查看
原文http://www.cnblogs.com/mz121star/archive/2012/11/14/jasmine3.html
旧版本中的一些matchers(匹配器)
and
所有的matchers匹配器支持添加
自定义(matchers)匹配器是一件很简单的事件,一个matcher(匹配器)函数使用 this.actual 接收到一个实际值,并且该匹配函数也可以包括0或多个参数。当实际值通过匹配器的匹配,你应当返回一个ture否则返回false。
以下代码定义了一个名为
将匹配器添加到suite中, 在before或者it代码块内调用
你可以自定义匹配失败的消息,在匹配函数中给this.message赋值即可实现
expect(x).toEqual(y);当x和y相等时候通过
expect(x).toBe(y);当x和y是同一个对象时候通过
expect(x).toMatch(pattern);x匹配pattern(字符串或正则表达式)时通过
expect(x).toBeDefined();x不是undefined时通过
expect(x).toBeUndefined();
x是
undefined时通过
expect(x).toBeNull();
x是null时通过
expect(x).toBeTruthy();x和true等价时候通过
expect(x).toBeFalsy();x和false等价时候通过
expect(x).toContain(y);x(数组或字符串)包含y时通过
expect(x).toBeLessThan(y);x小于y时通过
expect(x).toBeGreaterThan(y);x大于y时通过
expect(function(){fn();}).toThrow(e);函数fn抛出异常时候通过
旧版本中的一些matchers(匹配器)
toNotEqual,
toNotBe,
toNotMatch,
toNotContain将在以后被废除.建议使用
not.toEqual,
not.toBe,
not.toMatch,
and
not.toContainrespectively.
所有的matchers匹配器支持添加
.not反转结果:
expect(x).not.toEqual(y);
自定义Matchers(匹配器)
以上提供的Matchers(匹配器)已经可以满足你的大部分需求了,但是我们仍然推荐你按照需求定义自己的匹配器去匹配更加复杂的情况,自定义匹配器可以使你的代码意图更加明了,并且可以帮你移除重复的代码。自定义(matchers)匹配器是一件很简单的事件,一个matcher(匹配器)函数使用 this.actual 接收到一个实际值,并且该匹配函数也可以包括0或多个参数。当实际值通过匹配器的匹配,你应当返回一个ture否则返回false。
以下代码定义了一个名为
toBeLessThan()的匹配器:
toBeLessThan: function(expected) { return this.actual < expected; };
将匹配器添加到suite中, 在before或者it代码块内调用
this.addMatchers()
beforeEach(function() { this.addMatchers({ toBeLessThan: function(expected) { return this.actual < expected; } }); });
你可以自定义匹配失败的消息,在匹配函数中给this.message赋值即可实现
beforeEach(function() { this.addMatchers({ toBeLessThan: function(expected) { var actual = this.actual; var notText = this.isNot ? " not" : ""; this.message = function () { return "Expected " + actual + notText + " to be less than " + expected; } return actual < expected; } }); });
相关文章推荐
- Android 记录-点滴
- 使用IDEA将本地项目提交到svn上
- Windows 获取文件操作时间的方法
- C/C++常用算法【C语言顺序查找(顺序表)】【2】
- map的整理——面试问题
- php5.6的安装
- fastjson应用
- 【Solutions系列】Upsell Analysis,销售分析模版
- 解析平移动画本位的理解
- Angularjs 分页控件
- java中StringBuilder、StringBuffer、String类之间的关系
- Linux C编程---指针数组简析(二维数组、多级指针)
- Progressbar的使用以及ListView的使用
- //兼容苹果手机的SDK微信上传图片 下载到服务器
- 比 H5 应用多一点原生应用小一点,流应用能不能将 H5 应用真正推广起来?
- 1020. 月饼
- android 打包 eclipse 错误 Export aborted because fatal lint errors were found
- tomcat的Host配置
- IT资源专业搜索-www.easysoo.cn
- RadioGroup,RadioButton CheckBox控件的使用