2012-01-12 8 views
5

私はモックアウトしたい既存のプラグインを使用するプラグインを作成しています。私が書いているプラ​​グインは、次のようにソートの見えjQueryプラグインを模擬するにはどうすればよいですか?

(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; 
} 

実際のプラグインのインスタンスはまだ呼び出されています。

答えて

0

私はちょうどそれを行う方法を働いた、私はそれが最高だがわからないが、それは私の目的のために動作します。私は最近、ブートストラップモーダルでこれを経て、で固定

describe("When creating", function(){ 
    var element; 
    var passedOptions; 
    beforeEach(function(){ 
    $.widget("pluginWhichShouldBeMocked",{ 
     getOptions: function(){ 
     return this.options; 
     } 
    }); 
    element = $("<div id='extenalPlugin'></div>"); 
    element.myPlugin(); 
    }); 

    it("should create the other plugin and pass 'bar' to it as the foo parameter", function(){ 
    expect(element.find("div#externalPlugin").pluginWhichShouldBeMocked("getOptions").foo).toEqual("bar"); 
    }); 
}); 
0

は、私は私のアサートに使用できるオプションを取得する方法で、このようなテスト・セットアップでのモックプラグインを作成します。

beforeEach(()=>{ 
    jQuery.fn.modal =() => {} 
}) 

describe(()=>{ 
    \\ ... do your thing 
}) 

afterEach(()=>{ 
    delete jQuery.fn.modal 
}) 
関連する問題