Since the version 0.9-rc, we have added the methods mocking functionality, so you could have more wonderfull experience with the framework.
In general there are only two methods for the mocking stuffs. mockup(object, method_name, mock) (or mockUp(...))
to mock an object's method up and undoMockup(object, method_name) (undo_mockup(...)) to get the original method back.
testElementSwitchingOff: function() {
this.mockUp(Effect, 'BlindDown', function(element, options) {
$(element).show();
}
var some_element = new Element('div').hide();
this.assert_hidden(some_element);
Effect.BlindDown(some_element);
this.assert_visible(some_element);
},
testAnObjectMocking: function() {
// this should be somewhere in your code
Object.extend(Element.prototype, {
hideSmoothly: function() {
Effect.Fade(this, {duration: 0.4});
}
});
var some_element = new Element('div');
this.mockUp(some_element, 'hideSmoothly', function() {
some_element.style.display = 'none';
});
this.assert_visible(some_element);
some_element.hideSmoothly();
this.assert_hidden(some_element);
}
In general you can mockup an object's methods and a class prototype methods, but not that the object's level mocks have got a higher priority than the prototype ones. So if you have mocked up an prototype with one mock1 and the prototype object with mock2, then for all the prototype instances (future and existing ones) you'll have the mock1 working on, but for the object which you have mocked up with mock2, you'll have the mock2 function working.
var AClass = Class.create()
AClass.prototype = {
sayHello: function() {
alert('hello');
}
};
......
testAClassSayHello: function() {
var obj1 = new AClass();
var obj2 = new AClass();
this.mockUp(obj1, 'sayHello', function() {
alert('hi there');
});
this.mockUp(AClass.prototype, 'sayHello', function() {
alert('mocked hello');
});
var obj3 = new AClass();
obj1.sayHello(); // <- will say 'hi there'
obj2.sayHello(); // <- will say 'mocked hello'
obj3.sayHello(); // <- will say 'mocked hello'
}