views:

14

answers:

0

I have some functionality in a rails app switched on or off with the use of constants, and I'm using these in the controller class definitions, e.g.

class SampleController < ApplicationController
  if ONE
    def index
      render :text => '1'
    end
  else
    def index
      render :text => '2'
    end
  end
end

The constants are set in config files, and I'd like to be able to test both scenarios in my functional tests, so I've created a test specifically for this:

class SampleController < ActionController::IntegrationTest
  def setup
    @one = ONE
    unless ONE
      silence_warnings do
        Object.const_set('ONE', true)
      end
      load 'sample_controller'
    end
  end

  def teardown
    unless @one
      silence_warnings do
        Object.const_set('ONE', false)
      end
      load 'sample_controller'
    end
  end  
end

This works when the test is run in isolation, but not with the other tests, e.g. rake test:functionals. Is there any way of achieving what I'm after, or is it not a sensible way of doing things? Currently I've tweaked the 'test' task to change the constants by setting environment variables, then re-run the specific tests, but it seems messy. Presume it would work if I moved the test inside the method definition, but it seems more elegant to do the test when the class is loaded.