[rspec-users] Mocking/Stubbing ActiveRecord.config.default_timezone

James Byrne lists at ruby-forum.com
Mon Jan 26 10:23:20 EST 2009


In tracking down a bug in AuthLogic I realized that I had made the same
error in one of my libraries.  Knowing what the error was made creating
a test to expose it (using cucumber) rather trivial.  However, it has
occurred to me that this sort of issue is far more subtle than I first
appreciated.  So, I am looking for some opinions on how to test for this
sort of thing in a more general sense.

At issue is the setting of config values in environment.rb files and
their influence on user code.  In the specific case I encountered this
code demonstrates the essence of the situation:

    def active_row?
      time_now = DateTime.now
      return true if  (effective_from <= time_now and
                      (not superseded_after or superseded_after >=
time_now ))
      return false
    end

The error is that the time_now assignment is not testing whether or not
the default AR time is utc.  The correct code is:

    def active_row?
      if self.class.default_timezone == :utc
        time_now = DateTime.now.utc
      else
        time_now = DateTime.now
      end
      return true if  (effective_from <= time_now and
                      (not superseded_after or superseded_after >=
time_now ))
      return false
    end

My question is:  How does one vary the config.default_timezone, or any
configuration value for that matter, in a cucumber step definition or
RSpec test?
-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list