Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stubbing a class level constant in rspec

My class is structured something like this:

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

Now i want to unit test some_method_that_use_above_constant on the basis of different values ONE_CLASS_LEVEL_CONSTANT_BEING_READ_FROM_DB . Is this possible to stub out value of ONE_CLASS_LEVEL_CONSTANT_BEING_READ_FROM_DB, so that i can test it for different values in rspec ?

like image 441
Sahil Dhankhar Avatar asked Mar 16 '23 09:03

Sahil Dhankhar


1 Answers

As per this doc, with the version 2.11 of Rspec this should work: stub_const("Abc::ONE_CLASS_LEVEL_CONSTANT_BEING_READ_FROM_DB", 5)

like image 103
AmrataB Avatar answered Mar 29 '23 07:03

AmrataB