chai-param

使用 chai 验证你的方法参数

快速开始

在你的项目中安装它

npm install chai-param

引入并加载它

var chai = require('chai'),
    chaiParam = require('chai-param'),
    expect = chai.expect,
    should = chai.should(),
    param = chaiParam.param;

chai.use(chaiParam);

快乐的方法保护 :)

function register(user, pass) {
	// using should
	param(user, 'user', 'UsersCtl.register').should.not.be.empty;
	
	// or using expect
	expect(user).param('user', 'UsersCtl.register')
		.to.have.length.above(5)
		.and.below(16);
}

配置

Chai-param 可以通过在 chaiParam.config 对象属性中设置值来配置。例如

var chaiParam = require('chai-param');

chaiParam.config.improveMessages = true;

improveMessages

类型: Boolean 默认值: true

控制 chai-param 是否应该用更具描述性的信息替换 chai 的信息。因此,例如,你将不会得到

UserCtl.register: expected 'asdf' to have a length above 5 but got 4

你会得到…

Improved error message

在错误信息中显示调用者

类型: Boolean 默认值: true

此选项仅适用于 node,并且 **需要 improveMessagestrue**。

chaiParam.config.showCaller = true;

disableColors

类型: Boolean 默认值: false

禁用消息颜色。无聊,但对测试有用。