[mocha-developer] Stubbing Constants

s.ross cwdinfo at gmail.com
Mon Jul 23 11:50:23 EDT 2007

I wound up using:

class Module
   def redefine_const(name, value)
     __send__(:remove_const, name) if const_defined?(name)
     const_set(name, value)

The changing the constant on Object:

Object.redefine_const(:RAILS_ENV, 'production')

This fights with Mocha, but accomplishes my goal.


On Jul 23, 2007, at 1:21 AM, James Mead wrote:

> On 23/07/07, s.ross <cwdinfo at gmail.com> wrote:
>> This may sound odd, but here's the scenario: My Rails app behaves a
>> bit differently in development versus production mode. Because Ruby
>> won't let me redefine constants like RAILS_ENV I thought, hmmmmmmmm,
>> maybe I can just stub it and then write tests that were mode  
>> specific.
>> Is this something that can be done with Mocha?
> No. Not currently.
> I suggest you use Module#const_set for your tests and make sure you  
> reset at
> the end of the test OR setup a new environment which mirrors your
> production.rb file in which you can run tests.
> -- 
> James.
> http://blog.floehopper.org
> _______________________________________________
> mocha-developer mailing list
> mocha-developer at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mocha-developer

More information about the mocha-developer mailing list