2010-11-25 3 views
3

jQueryプラグインを書いている間、私はqunitを試していると私は、私は次のことをテストすることができますどのように思っていました:jQuery Plugin:qunitでプラグインの設定をテストするにはどうすればよいですか?

(function($){ 

    $.fn.myPlugin = function(options){ 
     var defaults = { 
      foo: function(){ 
       return 'bar'; 
      } 
     }; 

     options = $.extend(defaults, options); 

     return this.each(function(){ ... }); 
    }; 

})(jQuery); 

これは私のqunitテストの簡易版である:

module('MyPlugin: Configuration'); 

test('Can overwrite foo', function(){ 
    var mockFoo = function(){ 
     return 'no bar'; 
    }; 

    //equals(notsure.myPlugin({ foo: mockFoo }, 'no bar', 'Overwriting failed'); 
}); 

だから私はました私のテストの中のプラグインから内部メソッド/メンバーをどのように公開することができるのだろうか?

+0

多くの応答はありません。私は何か変わったことを尋ねましたか?私が質問を改善することができれば、私は提案に触れることができます。 – Pickels

答えて

5

私が賞金を払った後、私は本当に良いサイトを見つけました。どのようにして.data()を使用して気分の良いプロパティとメソッドを公開するのかを説明しました。

ここでは、ブログ投稿全体が見つかります:building object oriented jquery plugin

これはすべてのクレジットがブログ投稿の著者に行くように、上記のリンクの全体の例です。

(function($){ 
    var MyPlugin = function(element, options) 
    { 
     var elem = $(element); 
     var obj = this; 
     var settings = $.extend({ 
      param: 'defaultValue' 
     }, options || {}); 

     // Public method - can be called from client code 
     this.publicMethod = function() 
     { 
      console.log('public method called!'); 
     }; 

     // Private method - can only be called from within this object 
     var privateMethod = function() 
     { 
      console.log('private method called!'); 
     }; 
    }; 

    $.fn.myplugin = function(options) 
    { 
     return this.each(function() 
     { 
      var element = $(this); 

      // Return early if this element already has a plugin instance 
      if (element.data('myplugin')) return; 

      // pass options to plugin constructor 
      var myplugin = new MyPlugin(this, options); 

      // Store plugin object in this element's data 
      element.data('myplugin', myplugin); 
     }); 
    }; 
})(jQuery);