接上一篇,这一片我学习了ASSERTION STYLES断言部分
原文链接https://www.chaijs.com/guide/styles/
Assert
assert的APIhttps://www.chaijs.com/api/assert/
断言样式通过断言接口公开。这提供了经典的断言点表示法,与node.js打包的表示法类似。然而,这个断言模块提供了几个额外的测试,兼容所有浏览器。
var assert = require('chai').assert
, foo = 'bar'
, beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
assert.typeOf(foo, 'string'); // without optional message
assert.typeOf(foo, 'string', 'foo is a string'); // with optional message
assert.equal(foo, 'bar', 'foo equal `bar`');
assert.lengthOf(foo, 3, 'foo`s value has a length of 3');
assert.lengthOf(beverages.tea, 3, 'beverages has 3 types of tea');
断言样式最后一个参数是可选的,如果断言未通过,最后一个参数的信息将包含在错误消息中。
BDD
API官网https://www.chaijs.com/api/bdd/
BDD断言样式有两种样式,expect和should,两个都使用可链接语言,但是最初构造断言的方式有所不同。在特定条件下,应该有警告和克服警告的附加工具。
Expect
bdd样式通过expect或should接口公开。在这两个场景中,可以用自然语言链接断言;
var expect = require('chai').expect
, foo = 'bar'
, beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.have.lengthOf(3);
expect(beverages).to.have.property('tea').with.lengthOf(3);
Expect还允许您在可能发生的任何失败断言之前包含任意消息。
var answer = 43;
// AssertionError: expected 43 to equal 42.
expect(answer).to.equal(42);
// AssertionError: topic [answer]: expected 43 to equal 42.
expect(answer, 'topic [answer]').to.equal(42);
当与诸如布尔值或数字之类的非描述性主题一起使用时,这非常有用。
我试了一下,直接用,如果断言不通过就会报错
Should
官网部分:
should样式和expect一样,可以使用自然语言作为断言,但是会使用should属性来拓展每个对象,启动断言,而且在IE浏览器上会有兼容性问题,使用时要注意。
var should = require('chai').should() //actually call the function
, foo = 'bar'
, beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
foo.should.be.a('string');
foo.should.equal('bar');
foo.should.have.lengthOf(3);
beverages.should.have.property('tea').with.lengthOf(3);
Differences
expect require只是对expect函数的引用,而对于should require,函数正在被执行。
var chai = require('chai')
, expect = chai.expect
, should = chai.should();
Expect接口提供了一个函数作为链接语言断言的起点。它适用于node.js和所有浏览器
shoul接口扩展了object.prototype,以提供一个getter作为语言断言的起点。它适用于node.js和除IE之外的所有现代浏览器。
Should Extras
考虑到这应该通过扩展object.prototype来实现,有些场景不应该工作。主要是,如果您试图检查对象的存在。采用以下伪代码
db.get(1234, function (err, doc) {
// we expect error to not exist
// we expect doc to exist and be an object
});
假定err应为null或undefined,err.should.not.exist不是有效语句,跟should语法中已存在的null和undefined不一样。因此,这个场景的断言该这么写
var should = require('chai').should();
db.get(1234, function (err, doc) {
should.not.exist(err);
should.exist(doc);
doc.should.be.an('object');
});
下面有几个快速语句,使用should时可以帮你远离问题
should.exist
should.not.exist
should.equal
should.not.equal
should.Throw
should.not.Throw
Using Should in ES2015
在ES2015中import这个should函数之后需要执行
import chai from 'chai';
chai.should();
也可以这么写,看起来更简洁
import 'chai/register-should';
Configuration(一些配置)
config.includeStack
- @param {Boolean}
- @default
false
断言错误的消息里面是否包含堆栈跟踪。默认值为false(取消堆栈跟踪)
chai.config.includeStack = true; // turn on stack trace
config.showDiff
- @param {Boolean}
- @default
true
是否显示diff,默认为true
chai.config.showDiff = false; // turn off reporter diff display
config.truncateThreshold
- @param {Number}
- @default
40
截断阈值,默认是40,如果需要完全截断,修改为如下
chai.config.truncateThreshold = 0; // disable truncating