2016-08-31 21 views
1

我有一個Ruby-Cucumber框架。我需要使用方法method_missing來避免編寫一些重複功能。我應該在哪裏放置我的method_missing?在support文件夾下的任何.rb文件?或者它應該進入一些不同的特定文件?我應該在Cucumber Ruby框架中定義method_missing?

+0

請試試這裏:帖子標題爲:「覆蓋method_missing類和實例方法?」你可能會得到一些關於如何去做的想法。 http://stackoverflow.com/questions/22846777/override-method-missing-for-class-and-instance-methods – BKSpurgeon

回答

1

定義method_missing如您在黃瓜的輔助方法,在.rb文件中support目錄和模塊,通過它來傳遞給the Cucumber World在:

module MethodMissing 
    def method_missing(method) 
    puts "You said #{method}" 
    end 
end 
World MethodMissing 

然後,您可以調用步驟定義缺失的方法。

像任何黃瓜輔助方法一樣,method_missing只應在黃瓜World上定義。如果您將其定義在支持文件的頂層,那麼它將在Ruby頂層對象上定義,並且在任何地方都可用,這是不合理的,並且可能會破壞其他代碼。

我故意沒有聽從super,因爲World沒有定義method_missing,或定義respond_to_missing?,因爲我沒有任何計劃呼籲method(:foo)World,但如果你喜歡,你可以做這些事情當你定義method_missing時總是這樣做。

相關問題