Topic: Test a production configuration option in my test environment

I've created a limit on the number of model objects a user can create using my application over a period of 24 hours.  I don't want that limit to apply in the development or test environments - just in the production environment.  So I put a constant that represents the limit inside production.rb under the config/environment folder.

Now in one of my tests (not all of them), I'd like to test that the limit is enforced.

What's the best way to do that?

Re: Test a production configuration option in my test environment

I wouldn't try to switch environments for certain tests. Instead make it environment independent. Meaning, don't check what environment you're in, check for the existence of the constant. Then it's just a matter of setting the constant in the one test where you want to check the functionality. You may want to make it a variable of some kind as I'm not sure how well setting constants work in tests (they might persist between tests).

Railscasts - Free Ruby on Rails Screencasts

Re: Test a production configuration option in my test environment

So initially I had a constant defined in the production.rb file and I wanted to set it in the test.  But you can't set a constant dynamically in a test.  What's the best way to make it a variable that can be set dynamically but accessible across the controllers and the tests?  I was thinking of using a global variable but most of what i've read advocates against using them.

Re: Test a production configuration option in my test environment

How about a hash inside of a constant for general app configurations?

APP_CONFIG = { :foo => 'bar' }
APP_CONFIG[:foo] = 'blah'

That way you can access it everywhere and still change it. You may want to move this into a .yml file and load that (similar to database.yml).

Railscasts - Free Ruby on Rails Screencasts

Re: Test a production configuration option in my test environment

Thanks Ryan.  That works out well.

Re: Test a production configuration option in my test environment

I ran into this issue and figured out how I could use Module#const_set to set constants dynamically on either the model class (unit tests) or controller class (functional tests). I've laid it out in detail in a blog post at http://blog.geekdaily.org/2007/09/setting-constan.html

I'd love a critique of this methodology from more experienced Railers (Railsters? RoRers? wink which is darned near anyone at this point. =] Thanks in advance!

Re: Test a production configuration option in my test environment

Setting the RAILS_ENV variable dynamically in a test doesn't seem like a good solution to me. I imagine this would introduce some tricky bugs where some behavior is dependent on the environment which you don't expect. I haven't done any testing so maybe it's not problem, just something to watch out for.

Railscasts - Free Ruby on Rails Screencasts

Re: Test a production configuration option in my test environment

Tampering with RAILS_ENV certainly gave me pause, but grepping through the code of the rails-1.2.3 gem made me feel a little better. It seems that RAILS_ENV is properly segregated to commands, environment, and other bootstrapping bits (and the rails_info builtin).

This would make any side effects either my bad app code or my bad test code, either of which I can learn from and fix. You're absolutely right, though, that it shouldn't be approached without understanding what you're up to.