自定义toEqual

toEqual mathers 支持用户自定义比较方法,使用的是jasmine.addCustomEquallyTester(myCustomEqualltyFunction)方法注册,注册的时候只能在beforeEachbeforeAllit内注册。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的注册也只能发生在beforeEachbeforeAllit内。

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:一个matcherstoBeFoofy就是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 的自定义部分的更多相关文章

  1. angularjs自动化测试系列之jasmine

    angularjs自动化测试系列之jasmine jasmine参考 html <!DOCTYPE html> <html lang="en"> <h ...

  2. Karma+Jasmine实现自动化单元测试

    1.Karma介绍 Karma是Testacular的新名字,在2012年google开源了Testacular,2013年Testacular改名为Karma.Karma是一个让人感到非常神秘的名字 ...

  3. Jasmine入门(结合示例讲解)

    参考: http://www.cnblogs.com/wushangjue/p/4541209.html http://keenwon.com/1191.html http://jasmine.git ...

  4. JavaScript单元测试框架-Jasmine

    Jasmine的开发团队来自PivotalLabs,他们一开始开发的JavaScript测试框架是JsUnit,来源于著名的JAVA测试框架JUnit.JsUnit是xUnit的JavaScript实 ...

  5. 使用 Jasmine 进行测试驱动的 JavaScript 开发

    Jasmine 为 JavaScript 提供了 TDD (测试驱动开发)的框架,对于前端软件开发提供了良好的质量保证,这里对 Jasmine 的配置和使用做一个说明. 目前,Jasmine 的最新版 ...

  6. 前端测试框架 jasmine 的使用

    最近的项目在使用AngulaJs,对JS代码的测试问题就摆在了面前.通过对比我们选择了 Karma  + jasmine ,使用 Jasmine做单元测试 ,Karma 自动化完成,当然了如果使用 K ...

  7. angularJS测试一 Karma Jasmine Mock

    AngularJS测试 一 测试工具 1.NodeJS领域:Jasmine做单元测试,Karma自动化完成单元测试,Grunt启动Karma统一项目管理,Yeoman最后封装成一个项目原型模板,npm ...

  8. jasmine note

    jasmine 简介 jasmine 是一个测试驱动开发(TDD)测试框架, 一个js测试框架,它不依赖于浏览器.dom或其他js框架 jasmine有十分简洁的语法 使用 从 这里 下载 stant ...

  9. jasmine官方api参考

    jasmine 简介 jasmine 是一个行为驱动开发(TDD)测试框架, 一个js测试框架,它不依赖于浏览器.dom或其他js框架 jasmine有十分简介的语法 使用 从 这里 下载 stant ...

随机推荐

  1. (简单) POJ 3321 Apple Tree,树链剖分+树状数组。

    Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...

  2. CodeForces 622D Optimal Number Permutation

    是一个简单构造题. 请观察公式: 绝对值里面的就是 |di-(n-i)|,即di与(n-i)的差值的绝对值. 事实上,对于任何n,我们都可以构造出来每一个i的di与(n-i)的差值为0. 换句话说,就 ...

  3. linux中vi和vim编辑工具

    linux中知名的还有emacs,功能比vim还要强大 vim 如果文件存在vim是打开这个文件,若果不存在,则先新建再打开 命令模式:任何模式都可以通过Esc回到命令模式,命令模式可以通过命令进行选 ...

  4. 编写高质量JavaScript代码的基本要点记录

    原文:深入理解JavaScript系列(1):编写高质量JavaScript代码的基本要点 1.最小全局变量(Minimizing Globals)的重要性 JavaScript通过函数管理作用域.在 ...

  5. IOS9中联系人对象的使用及增删改查操作的函数封装

    之前克服重重困难把IOS9中新的类联系人搞明白了,现在把增删改查封装成了函数,如下: // // ViewController.m // IOS9中联系人CNContact的使用 // // Crea ...

  6. C# new和override的区别

    浅析C# new和override的区别 C#中new和override是继承中经常用到的两个关键字,但是往往有时候容易把这两个关键字的作用搞混淆. new C# new关键字表示隐藏,是指加上new ...

  7. 中英文混合字符串截取java

    //截取字符串长度(中文2个字节,半个中文显示一个) public String subTextString(String str,int len){ if(str.length()<len/2 ...

  8. IOS 创建和设置pch

    1.添加pch文件 2.修改工程配置文件 Building Settings->All->Apple LLVM 6.0 -Language -> Prefix Header

  9. php循环生成的表单如何获得其各项值案例

    思路:输入框和按钮是用for循环生成的,不但要获取输入框里的各项值,并且要获取点击按钮的值,要知道是那个按钮被点击了,这里以生成5个为例.如图: 这是提交页面,点击的是“小米”. 这是显示结果,测试显 ...

  10. iOS 之 绘图简介

    iOS 实现图形编程主要有三种技术:UIKit.Core Graphics.OpenGL. 绘图需要在图形环境中进行,图形环境分为三种:屏幕图形环境.off screen 位图环境和PDF图形环境.在 ...