Jasmine 的自定义部分
自定义toEqual
toEqual mathers 支持用户自定义比较方法,使用的是jasmine.addCustomEquallyTester(myCustomEqualltyFunction)方法注册,注册的时候只能在beforeEach、beforeAll或it内注册。myCustomEqualltyFunction接受两个参数,就是需要比较的值。比较完成后必须返回true或false。
describe("custom equality", function() {
var myCustomEquality = function(first, second) {
if (typeof first == "string" && typeof second == "string") {
return first[0] == second[1];
}
};
beforeEach(function() {
jasmine.addCustomEqualityTester(myCustomEquality);
});
it("should be custom equal", function() {
expect("abc").toEqual("aaa");
});
it("should be custom not equal", function() {
expect("abc").not.toEqual("abc");
});
});
默认的toEqual被我们注册的比较方法给取代了。
自定义 mathers
mathers是一个定义了mathers function方法的对象,这个方法需要返回一个有campare属性的对象。同样的,使用 jasmine.addMatchers进行mathers的注册也只能发生在beforeEach、beforeAll或it内。
var customMatchers = {
toBeGoofy: function(util, customEqualityTesters) {
return {
compare: function(actual, expected) {
if (expected === undefined) {
expected = '';
}
var result = {};
result.pass = util.equals(actual.hyuk, "gawrsh" + expected, customEqualityTesters);
if (result.pass) {
result.message = "Expected " + actual + " not to be quite so goofy";
} else {
result.message = "Expected " + actual + " to be goofy, but it was not very goofy";
}
return result;
}
};
}
};
describe("Custom matcher: 'toBeGoofy'", function() {
beforeEach(function() {
jasmine.addMatchers(customMatchers);
});
it("is available on an expectation", function() {
expect({
hyuk: 'gawrsh'
}).toBeGoofy();
});
it("can take an 'expected' parameter", function() {
expect({
hyuk: 'gawrsh is fun'
}).toBeGoofy(' is fun');
});
it("can be negated", function() {
expect({
hyuk: 'this is fun'
}).not.toBeGoofy();
});
});
- customMatchers:一个对象,可以包含多个方法作为
matchers; - toBeFoofy:一个
matchers,toBeFoofy就是matchers调用时的名字。有参数util和customEqualityTesters(自定义toEqual)。进行matcher的时候都会执行这个方法。这个方法还需要返回一个有compare方法的对象才能正常执行; - compare:用作比较actual(实际)和expected(期望)。可以返回一个有pass和message属性对象,pass为true就是通过,pass为false就是不通过,message就是不通过的时候的提示信息。
matchers的.not操作默认是取compare.pass的否定,如果想自定义.not的比较器,可以在compare方法后添加negativeCompare方法,当然,negativeCompare是可选的。
自定义reporter
在命令行敲下jasmine命令后会有一串的反馈信息,这串信息其实也是可以使用jasmine.getEnv().addReporter(myReporter);自定义的,注册时候建议使用全局定义。
myReporter是一个对象,它可以有以下方法:
- jasmineStarted(suiteInfo):会在全部
specs读取完成后,和execution开始前执行; - jasmineDone():会在整个套件完成后执行;
- suiteStarted(result)/suiteDone(result):每个
describe开始/结束的时候会执行; - specStarted(result)/specDone(result):每个
it开始/结束的时候会执行。
var myReporter = {
jasmineStarted: function(suiteInfo) {
console.log('Running suite with ' + suiteInfo.totalSpecsDefined);
},
suiteStarted: function(result) {
console.log('Suite started: ' + result.description + ' whose full description is: ' + result.fullName);
},
specStarted: function(result) {
console.log('Spec started: ' + result.description + ' whose full description is: ' + result.fullName);
},
specDone: function(result) {
console.log('Spec: ' + result.description + ' was ' + result.status);
for (var i = 0; i < result.failedExpectations.length; i++) {
console.log('Failure: ' + result.failedExpectations[i].message);
console.log(result.failedExpectations[i].stack);
}
console.log(result.passedExpectations.length);
},
suiteDone: function(result) {
console.log('Suite: ' + result.description + ' was ' + result.status);
for (var i = 0; i < result.failedExpectations.length; i++) {
console.log('AfterAll ' + result.failedExpectations[i].message);
console.log(result.failedExpectations[i].stack);
}
},
jasmineDone: function() {
console.log('Finished suite');
}
};
jasmine.getEnv().addReporter(myReporter);
describe('Top Level suite', function() {
it('spec', function() {
expect(1).toBe(1);
});
describe('Nested suite', function() {
it('nested spec', function() {
expect(true).toBe(true);
});
});
});
Jasmine 的自定义部分的更多相关文章
- angularjs自动化测试系列之jasmine
angularjs自动化测试系列之jasmine jasmine参考 html <!DOCTYPE html> <html lang="en"> <h ...
- Karma+Jasmine实现自动化单元测试
1.Karma介绍 Karma是Testacular的新名字,在2012年google开源了Testacular,2013年Testacular改名为Karma.Karma是一个让人感到非常神秘的名字 ...
- Jasmine入门(结合示例讲解)
参考: http://www.cnblogs.com/wushangjue/p/4541209.html http://keenwon.com/1191.html http://jasmine.git ...
- JavaScript单元测试框架-Jasmine
Jasmine的开发团队来自PivotalLabs,他们一开始开发的JavaScript测试框架是JsUnit,来源于著名的JAVA测试框架JUnit.JsUnit是xUnit的JavaScript实 ...
- 使用 Jasmine 进行测试驱动的 JavaScript 开发
Jasmine 为 JavaScript 提供了 TDD (测试驱动开发)的框架,对于前端软件开发提供了良好的质量保证,这里对 Jasmine 的配置和使用做一个说明. 目前,Jasmine 的最新版 ...
- 前端测试框架 jasmine 的使用
最近的项目在使用AngulaJs,对JS代码的测试问题就摆在了面前.通过对比我们选择了 Karma + jasmine ,使用 Jasmine做单元测试 ,Karma 自动化完成,当然了如果使用 K ...
- angularJS测试一 Karma Jasmine Mock
AngularJS测试 一 测试工具 1.NodeJS领域:Jasmine做单元测试,Karma自动化完成单元测试,Grunt启动Karma统一项目管理,Yeoman最后封装成一个项目原型模板,npm ...
- jasmine note
jasmine 简介 jasmine 是一个测试驱动开发(TDD)测试框架, 一个js测试框架,它不依赖于浏览器.dom或其他js框架 jasmine有十分简洁的语法 使用 从 这里 下载 stant ...
- jasmine官方api参考
jasmine 简介 jasmine 是一个行为驱动开发(TDD)测试框架, 一个js测试框架,它不依赖于浏览器.dom或其他js框架 jasmine有十分简介的语法 使用 从 这里 下载 stant ...
随机推荐
- PHPCMS v9 列表页实现文件下砸
{template "content","header"} <div class="list"> <div class=& ...
- LPC1788的IIC使用
#ifndef __IIC0_H_ #define __IIC0_H_ #include "common.h" #include "delay.h" //IIC ...
- Spring MVC 关于分页的简单实现
据本人了解,目前较常用的分页实现办法有两种: 1.每次翻页都修改SQL,向SQL传入相关参数去数据库实时查出该页的数据并显示. 2.查出数据库某张表的全部数据,再通过在业务逻辑里面进行处理去取得某些数 ...
- ELF文件格式分析--结构篇
ELF文件格式,全称为Excutable and Linking Format,是一个开放的可执行文件和链接文件格式,在LINUX上很流行,跨平台软件的设计也多以ELF格式作为标准,其结构扩展性兼容性 ...
- IOS开发-OC学习-常用功能代码片段整理
IOS开发-OC学习-常用功能代码片段整理 IOS开发中会频繁用到一些代码段,用来实现一些固定的功能.比如在文本框中输入完后要让键盘收回,这个需要用一个简单的让文本框失去第一响应者的身份来完成.或者是 ...
- iOS tableViewCell plane格式下,接近section边缘不显示分割线却被复用解决办法
今天做公司产品的时候遇到了如题问题,困扰我很长时间,用尽各种办法不能解决,究其原因不知为何,自定义cell低端有view划线的时候,划线一般的显示1像素,而贴着section的显示很少 顶多0.3像素 ...
- 测试部署nginx
1.已经安装了nginx 2.修改tomcat端口号为10081(可随意) 3 修改nginx的nginx.config 如下: upstream apiserver { ip_ ...
- C语言-基础
程序是为了让计算机完成某项任务而编写的逐条执行的指令序列. C语言的特点是:结构化,语言简洁,功能强大,移植性好. 因为移植性好,所以大多数单片机如51.stm32,msp430,等众多嵌入式微处理器 ...
- 报错:Can't bind to local 8647 for debugger
修改8600为8601,并且勾上Use ADBHOST
- linux 套接字编程入门--Hello World
下述代码是linux套接字编程的入门代码.分为服务端和客户端源码. 服务端代码的主要流程是绑定ip地址和端口号建立套接字,等待客户端发起访问.接受客户端请求之后,向客户端发送字符串"hell ...