2016-01-08 144 views
0

我有一个简单的混入:使用自定义功能扩展should.js

var _ = require('lodash') 
_.mixin(require('lodash-uuid')) 

,我可以在代码中使用像这样:

if (_.isUuid(someValue)) { 
    // it's a uuid! 
} 

我希望能够延长基于测试,以利用该模块,例如:

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 

回答

2

您使用不正确的方法。您使用的方法是添加应吸气剂到任何对象。您需要使用should.Assertion.add,如:

var should = require('should'); 

should.Assertion.add('uuid', function() { 
    this.params = { operator: 'to be UUID' }; 

    this.assert(_.isUuid(this.obj)); 
}); 
+0

不错!谢谢。 Should.js文件没有留下任何迹象表明,这将是做到这一点的方法...... – brandonscript

+0

是的,可能。我会尽力在稍后解决。 –