[rspec-users] RSpec Testing ActiveRecord config dependency.

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


David Chelimsky wrote:

> Now this violates a principle that you shouldn't stub things on the
> object you're spec'ing, but the alternative is to play with global
> values, which violates other principles. This is the simplest way IMO.
> But if you're concerned about internals of AR changing, you could do
> this:

Since my library extends ActiveRecord::Base and its magic datetime 
columns I think that I should test that my extension does not change the 
anticipated behaviour of AR itself.

However, default_timezone cannot be set on an instance of AR:Base 
insofar as I can tell.  Further, setting the default_timezone on AR:Base 
itself does not appear to have any effect:

    [ "local", "utc" ].each do |tz|
      ActiveRecord::Base.default_timezone = tz
      @my_mage = Magik.new
      @my_mage.description = tz
      @my_mage.my_time_local = DateTime.now
      @my_mage.my_time_utc = DateTime.now.utc
      @my_mage.save!
      puts @my_mage.description
    end
...
  it "should set the UTC time (for _at/_on) correctly" do
    my_magic = Magik.find_by_description!("local")
    puts " "
    puts my_magic.description
    puts "Local: #{my_magic.my_time_local} = 
#{my_magic.my_time_local.to_f}"
    puts "  now: #{Time.now} = #{Time.now.to_f}"
    puts "  UTC: #{my_magic.my_time_utc} = #{my_magic.my_time_utc.to_f}"
    puts "  now: #{Time.now.utc} = #{Time.now.utc.to_f}"
    my_magic = Magik.find_by_description!("utc")
    puts " "
    puts my_magic.description
    puts "Local: #{my_magic.my_time_local} = 
#{my_magic.my_time_local.to_f}"
    puts "  now: #{Time.now} = #{Time.now.to_f}"
    puts "  UTC: #{my_magic.my_time_utc} = #{my_magic.my_time_utc.to_f}"
    puts "  now: #{Time.now.utc} = #{Time.now.utc.to_f}"

Produces this:

.....
local
Local: Mon Feb 09 14:20:06 UTC 2009 = 1234189206.0
  now: Mon Feb 09 14:20:06 -0500 2009 = 1234207206.77585
  UTC: Mon Feb 09 19:20:06 UTC 2009 = 1234207206.0
  now: Mon Feb 09 19:20:06 UTC 2009 = 1234207206.77609

utc
Local: Mon Feb 09 14:20:06 UTC 2009 = 1234189206.0
  now: Mon Feb 09 14:20:06 -0500 2009 = 1234207206.77807
  UTC: Mon Feb 09 19:20:06 UTC 2009 = 1234207206.0
  now: Mon Feb 09 19:20:06 UTC 2009 = 1234207206.7783
.

Finished in 0.429892 seconds

It seems that I am not overriding the AR:BASE.default_timezone setting 
since, as far as I can tell, the values stored are the same whatever 
value I set the default_tz to.

Does anyone have a technique to accomplish this?


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


More information about the rspec-users mailing list