[rspec-users] RSpec Testing ActiveRecord config dependency.

James Byrne lists at ruby-forum.com
Mon Feb 9 10:12:22 EST 2009

I have the following library code:

    def normal_time_now
      return DateTime.now.utc if default_timezone == :utc
      return DateTime.now

This is dependent upon a setting in config/environment.rb

  # Make Active Record use UTC-base instead of local time
  config.active_record.default_timezone = :utc

I want to test that I get the expected results with the config set to
utc and otherwise.  The library code is used to automatically set a
model attribute on create.  My existing specification looks somewhat
like this:

describe "Builds a Model with custom magic columns" do
  before(:all) do
    build_model   :magiks do
      string    :description
    # these are Rails' own magic columns
    # these are our custom magic columns
    @my_mage = Magik.new

  it "should set each custom magic column present" do
    (Magik.column_names & ActiveRecord::Base::HLL_AUDIT_COLUMNS).each do
      @my_mage.read_attribute(magic_column).should_not be_nil

I can simply create duplicate separate specification file and set the
configuration value appropriately in each. Something along the lines of:

describe "Builds a Model with custom magic columns" do
  before(:all) do
  config.active_record.default_timezone = nil
  config.active_record.default_timezone = :utc

But this strikes me as inappropriate.  I believe that this test belongs
inside the basic specification file but I cannot conceive of how to do
this.  Basically, I require some form of variable setup routine. Does
anyone have any suggestions on how to handle this case?

Posted via http://www.ruby-forum.com/.

More information about the rspec-users mailing list