Я пишу плагин, который использует существующий плагин, который я хотел бы макетировать.
Плагин, который я пишу, выглядит примерно так:
(function($){
$.widget("myPlugin",{
_create: function(){
var otherControl = $("<div></div>");
otherControl.pluginWhichShouldBeMocked({foo: "bar"});
this.element.append(otherControl);
}
});
})(jQuery);
И яесть тест Жасмин, который выглядит примерно так:
describe("When creating", function(){
var element;
var passedOptions;
beforeEach(function(){
jQuery.pluginWhichShouldBeMocked = function(options){
passedOptions = options;
}
element = $("<div></div>");
element.myPlugin();
});
it("should create the other plugin and pass 'bar' to it as the foo parameter", function(){
expect(passedOptions.foo).toEqual("bar");
});
});
В этой строке я попытался смоделировать плагин:
jQuery.pluginWhichShouldBeMocked = function(options){
passedOptions = options;
}
Хотя фактический экземпляр плагина все еще вызывается.