2013-02-04 29 views
0

我一直在爲我的Dojo窗口小部件創建一些測試來檢查布爾標誌是否正確設置。但是,我發現,因爲我已經改變了我的構造函數來傳入一個對象,以前運行測試似乎會影響後續測試。doh測試後dojo窗口小部件變量引用仍然保留

我已經嘗試銷燬拆卸方法中的小部件,但無論我如何做,值仍然存在。

任何人都可以建議我可能會做錯什麼嗎?

我的部件代碼:

var showControls = true; 

    return declare([WidgetBase, TemplatedMixin, _WidgetsInTemplateMixin], { 

     templateString: template, 

     constructor: function (params) { 

      this.showControls = (typeof params.showControls === "undefined" || typeof params.showControls != "boolean") ? this.showControls : params.showControls; 
     } 
    }); 

我的測試類是:

var customWidget; 

doh.register("Test controls", [ 
    { 
     name: "Test controls are not visible when set in constructor", 
     runTest: function() { 
      var params = { showControls: false }; 
      customWidget = new CustomWidget(params); 
      doh.assertFalse(customWidget.getShowControls()); 
     } 
    }, 
    { 
     name: "Test controls are visible when set in constructor with string instead of boolean", 
     runTest: function() { 
      var params = { showControls: "wrong" }; 
      customWidget= new CustomWidget(params); 
      doh.assertTrue(customWidget.getShowControls()); 
     } 
    } 
]); 

所以,第一個測試通過,作爲showControls設置爲false,但第二次測試嘗試創建一個新的實例,其中構造函數將檢查該值是一個布爾值。然而,當我調試這個,它認爲showControls開始爲'假',不正確。

任何線索?

感謝

回答

1

dijit/_WidgetBasea mechanism of mixing in constructor parameters,這是您所描述的行爲的原因。一個可能的解決方案是定義一個定製的setter的方法_set[PropertyName]Attr

var defaults = { 
    showControls: true 
} 

var CustomWidget = declare([_WidgetBase, _TemplatedMixin], { 
    templateString: "<div></div>", 

    constructor: function(params) { 
     declare.safeMixin(this, defaults); 
    }, 

    _setShowControlsAttr: function(value) { 
     this.showControls = (typeof value === "boolean") ? value : defaults.showControls; 
    } 
}); 

看到它在行動:http://jsfiddle.net/phusick/wrBHp/

+0

謝謝phusick,我試過你的建議,它的工作原理。我仍然試圖弄清楚它是如何工作的,說實話以及如何確保params對象中的屬性(在本例中爲params.showControls)實際上會在私有設置器中設置。 Dojo如何知道如何匹配這兩個? – IpponSolutions

0

我會建議你列出你的小部件的任何成員,如果你不,傳入構造函數的東西可能無法正確識別。看來你想使用this.showControls,所以你應該有一個showControls成員。像這樣:

return declare([WidgetBase, TemplatedMixin, _WidgetsInTemplateMixin], { 

    templateString: template, 

    showControls: true, // default value 

    constructor: function (params) { 
     // no further action, params are automatically mixed in already 
    } 
}); 

上市會員時要小心,道場解釋數組和對象爲類的成員(如Java中靜態的,據我所知他們連接到原型),所以如果您希望每個對象都如,單獨的數組值,將其列爲空並在您的構造函數中進行初始化。

+0

這不起作用(http://jsfiddle.net/phusick/BzZQB),因爲在構造函數參數中混合發生在'constructor','create'和'postMixInProperties'之後和'postCreate'之前,所以無論你在構造函數,它會在實例化後通過'_applyAttributes'方法返回。其他的可能性,如果你想避免使用setter/getters,就是在'postMixInProperties'中像'http://jsfiddle.net/phusick/ZupZE'那樣改變'this.params'對象,但是這比setter /名爲'defaults'的對象中的getter和default參數。 – phusick

+0

你想在這裏做什麼?在該小提琴中,this.showControls是真實的,「錯誤的」和未定義的(實際上拋出一個錯誤,所以你可能想改變構造函數中的那一行)你想要的行爲是什麼?你爲什麼期望this.showControls已經被設置? 在您的第一個代碼塊中,您是否認爲var showControls = true附加到您的聲明中?看看我的代碼示例,設置一個默認值,只是做showControls:true, –

+0

我不能確定@ user2010651是關於實現,但是因爲您對我的評論作出反應,我會提供我的見解:if showControls '構造函數的第一個參數對象的屬性是'boolean',然後將實例屬性'showControls'設置爲這個值(true || false),否則將其設置爲默認值(這是true)。你的代碼將它設置爲任何傳遞值,因此它不會通過提供的單元測試。 – phusick

相關問題