2012-03-23 44 views
3

我有幾個項目中的功能啓用機制(一個使用RSpec 1與Rails 2,一個與RSpec 2/Rails 3),我是尋找一種更好的方法來覆蓋一個功能,而無需對其他功能進行任何操作。最終,我正在尋找一種方法來存儲一個方法,當它被一個特定的參數調用時,並且以其他方式正常運行。我可以重寫一個方法在RSpec只有一個參數調用

關閉:

Project::Config.should_receive(:feature_enabled?).with('new_feature').any_number_of_times.and_return(true) 
get :index # calls feature_enabled? for a few other features too 

>> <Project::Config (class)> expected feature_enabled? with 'new_feature' but received it with 'some old feature' 

的作品,但我正在尋找的東西乾淨了一點,尤其是在情況下,有可能是前{}在不同層面實現不同的功能塊數。

Project::Config.should_receive(:feature_enabled?).with('new_feature').any_number_of_times.and_return(true) 
# expect any other calls 
Project::Config.should_receive(:feature_enabled?).any_number_of_times 
# I can't override any features here. This is annoying if I try to set up expectations at a few different levels 
get :index # might call feature_enabled? for a few features 

這也將失敗:

Project::Config.stub(:feature_enabled?).with('new_feature').and_return(true) 
get :index 

>> undefined method `feature_enabled?' for Project::Config:Class 

理想我能夠做到在同一行的東西,不影響其他FEATURE_ENABLED?調用。如果有什麼東西只適用於RSpec2,那很好,因爲我們會在某個時候更新其他項目。

回答

3

我發現通過打電話給原來的方法最好的解決方案的工作正是如此:

method = Project::Config.method(:feature_enabled?) 

Project::Config.stub(:feature_enabled?).and_return do |arg| 
    method.call(arg) 
end 

Project::Config.should_receive(:feature_enabled?).with('new_feature').and_return(true) 
+0

真棒 - 我每次運行期間存儲一點點額外的狀態跟蹤的是那些獲得覆蓋多種功能,但這是完美的。謝謝丹尼爾! – 2012-04-27 19:22:14

+0

沒問題。我一直在尋找清理方法,但唉,我還沒有找到更好的方法。 – 2012-04-27 20:46:33

相關問題