Javascript 在节点中测试绑定函数
在模块的create方法中,我将函数绑定到变量Javascript 在节点中测试绑定函数,javascript,node.js,Javascript,Node.js,在模块的create方法中,我将函数绑定到变量 var __ = function() {}; __.create = function() { var instance = new __(); instance.bound = instance.functionToBindTo.bind(instance, 'boundParameter'); return instance; } __.prototype.functionToBindTo = function(param
var __ = function() {};
__.create = function() {
var instance = new __();
instance.bound = instance.functionToBindTo.bind(instance, 'boundParameter');
return instance;
}
__.prototype.functionToBindTo = function(paramater1, parameter2) {
//do stuff
}
我现在希望能够测试调用“绑定”将“boundParameter”设置为parameter1
通常我会做一些像
'ensure parameter1 passed as first parameter' : function(test) {
var newInstance = ClassToTest.create();
newInstance.functionToBindTo = function(parameter1) {
test.equal(parameter1, 'boundParameter');
};
newInstance.bound();
test.done();
}
但是,由于.bind()实际上创建了一个新函数,所以这不起作用,因为我无法在测试中重写它。我知道可以选择使用我自己的bind2方法并修补它,但我希望有办法避免这种情况
有什么想法吗
谢谢
Matt这可能不是正确的方法,但是对于上面的测试用例,您可以覆盖
ClassToTest
原型对象中的函数tobindto
:
'ensure parameter1 passed as first parameter' : function(test) {
ClassToTest.prototype.functionToBindTo = function(parameter1) {
test.equal(parameter1, 'boundParameter');
};
var newInstance = ClassToTest.create();
newInstance.bound();
test.done();
}