我有一个简单的 lodash混合:
var _ = require('lodash')
_.mixin(require('lodash-uuid'))
我可以在代码中使用它,如下所示:
if (_.isUuid(someValue)) {
// it's a uuid!
}
我希望能够延长should.js基于 - 的测试来利用此模块,例如:
response.should.have.property('uuid').which.is.a.uuid()
docs对于延伸应该轻一点;我尝试过:
var uuid = should.extend('uuid', _.isUuid)
但这会抛出:
TypeError: response.should.have.property(...).which.is.a.uuid is not a function
最佳答案
你使用了不正确的方法。您使用的方法是向任何对象添加 should getter。您需要使用 should.Assertion.add
,例如:
var should = require('should');
should.Assertion.add('uuid', function() {
this.params = { operator: 'to be UUID' };
this.assert(_.isUuid(this.obj));
});
关于javascript - 使用自定义函数扩展 should.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34683527/