在 rspec 中插入一个 class 级别常量

Stubbing a class level constant in rspec

我的 class 结构如下:

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

现在我想根据不同的值 ONE_CLASS_LEVEL_CONSTANT_BEING_READ_FROM_DB 进行单元测试 some_method_that_use_above_constant 。 是否可以存根 ONE_CLASS_LEVEL_CONSTANT_BEING_READ_FROM_DB 的值,以便我可以针对 rspec 中的不同值测试它?

根据 this doc,对于 Rspec 的 2.11 版本,这应该可以工作: stub_const("Abc::ONE_CLASS_LEVEL_CONSTANT_BEING_READ_FROM_DB", 5)