2016-01-08 141 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

是的,可能。我會盡力在稍後解決。 –