惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
C
Check Point Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
博客园 - 司徒正美
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
V
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
O
OpenAI News
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
I
InfoQ
D
Docker
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - Franky
B
Blog
Scott Helme
Scott Helme
博客园 - 叶小钗
D
DataBreaches.Net

博客园 - Zhuang miao

使用bootstrap和metroui设计的微网站或手机app界面 利用Mahout实现在Hadoop上运行K-Means算法 大连二手汽车培训网上线 淘宝开放平台API调用nodejs实现 Nodejs+express+angularjs+mongodb搭建前端项目框架NJBlog 淘宝UED前端智勇大冲关第二季 CC.net&Nant配置文件 介绍一个款可以在javascript对象上实现观察者模式的类库-Watch.js 开发nodejs模块并发布到npm的简单示例 用jsTestDriver运行jasmine cases 使用Jasmine测试你的Javascript(二)之 Suites和specs 使用Jasmine测试你的Javascript(一)之 Jasmine简介 用SignalR创建实时永久长连接异步网络应用程序 Javascript实现图片的预加载的完整实现 常见前端面试题【转】 更改页面背景的jquery插件 javascript面向对象中的对象创建、继承、封装等实现方式 12款华丽的Admin管理后台模板 AOP
使用Jasmine测试你的Javascript(三)之 Matchers
Zhuang miao · 2012-11-14 · via 博客园 - Zhuang miao

使用Jasmine测试你的Javascript(一)之 Jasmine简介

使用Jasmine测试你的Javascript(二)之 Suites和specs

通过前两篇我们已经基本可以简单的用jasmine写出一段测试代码,本篇我们将来继续介绍jasmine中的matchers。

Jasmine提供了一些内置的matchers(匹配器)供你使用,下面有几个是你会经常用到的。

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.toContain respectively.

所有的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;
}
});
});