自定义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);
});
});
});

最新文章

  1. Python json.dumps 特殊数据类型的自定义序列化操作
  2. MySQL数据库1067 问题
  3. 动画--问题追踪:ImageView执行缩放动画ScaleAnimation之后,图像显示不全的问题。
  4. Win7 64下Visual C++ 6.0不兼容
  5. WPF实现强大的动态公式计算
  6. [笔记]PHP文件系统处理
  7. Legacy安装win7和Ubuntu14.04双系统
  8. PHP重构之函数上移
  9. 开发团队在TFS中使用Git Repository (一)
  10. jvm007 jvm知识点总览
  11. MongoDB Driver 简单的CURD
  12. SqlServer存储过程及函数
  13. xpath爬取新浪天气
  14. Kotlin基础(四)Lambda编程
  15. hive启动方式
  16. Python2和Python3中print的不同点
  17. 码云 使用 汉化 GitHub
  18. SQL注入之Sqli-labs系列第二十三关(基于过滤的GET注入)
  19. MyBatis 通用Mapper接口 Example的实例
  20. 解决ListView中Item的子控件与Item点击事件冲突

热门文章

  1. iOS开发之隐藏tabbar--解决隐藏后依然不能响应对应位置事件的问题
  2. object - c 在URL中截取特定参数的值
  3. matlab unique 顺序不变
  4. S3C2440 TFTLCD驱动详解
  5. C++的封装性
  6. 为什么建立TCP连接需要三次握手,为什么断开TCP连接需要四次握手,TIME_WAIT状态的意义
  7. UVa 10360 - Rat Attack
  8. java细节,细的你想象不到
  9. CSS3 Transitions 你可能不知道的知识点
  10. ajax 跨域了 cors