2015-04-17 35 views
4

我班不變的結構是這樣的:打樁一類水平rspec的

class Abc 
    ONE_CLASS_LEVEL_CONSTANT_BEING_READ_FROM_DB = GloablAttributeValue.read_from_db 
    def some_method_that_use_above_constant 
     # this function behaves differently for different values of ONE_CLASS_LEVEL_CONSTANT_BEING_READ_FROM_DB 
    end 
end 

現在我想單元測試some_method_that_use_above_constant不同的值ONE_CLASS_LEVEL_CONSTANT_BEING_READ_FROM_DB的基礎上。 是否可以將ONE_CLASS_LEVEL_CONSTANT_BEING_READ_FROM_DB的值剔除,以便我可以在rspec中測試不同的值?

回答

7

按照this doc,使用RSpec的2.11版本,這應該工作: stub_const("Abc::ONE_CLASS_LEVEL_CONSTANT_BEING_READ_FROM_DB", 5)

+1

感謝,聽起來有希望讓我試試,我會更新一次測試。 –

+1

這個作品很神奇!謝謝。我已將它標記爲預期的答案! –