Moock demo
Shows how one can mock methods with mook
JavaScript
function getStub(value){
function Stub(){
Stub.called = true;
Stub.args = arguments;
if (typeOf(value) == 'function') return value.apply(Stub,arguments);
return value;
}
Stub.called = false;
Stub.args = [];
return Stub;
}
Class.Mutators.Mock = function(methods){
for (var name in methods){
if (!(methods.hasOwnProperty(name))) continue;
this.prototype[name] = getStub(methods[name]);
}
};
/* Demo Starts Here! */
var ClassA = new Class({
initialize : function(){
this.a('a','b');
console.log(this.b());
}
, a : function(){
/* do something */
}
, b : function(){
/* return something */
}
});
var MockA = new Class({
Extends : ClassA
, Mock :{
a : null
, b: "bbb"
}
});
var mock = new MockA; //will log bbb
console.log(mock.a.called); //true because it was called from init
console.log(mock.a.args); //will log the args that were passed from init