# AR:BASE.default\_timezone

**URL:** <https://www.ruby-forum.com/t/ar-base-default-timezone/159889>\
**Category:** Rails\
**Created:** [February 9, 2009, 8:30pm UTC](https://www.ruby-forum.com/t/ar-base-default-timezone/159889 "2009-02-09T20:30:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [February 9, 2009, 8:30pm UTC](https://www.ruby-forum.com/t/ar-base-default-timezone/159889/1 "2009-02-09T20:30:45Z")

</div>

I have a requirement to test code whose execution depends upon the  
default\_timezone setting. I have tried several approaches to this but  
nothing that I do in my test seems to alter the behaviour that is set in  
environment.rb.

Is there a way to dynamically alter AR’s method of storing datetime  
during a test? Currently I attempt to do this in this fashion:

```
["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

```

but the contents of the my\_time\_local and my\_time\_utc do not seem to be  
what I expect:

default\_timezone = local  
Local: Mon Feb 09 14:20:06 UTC 2009 = 1234189206.0 \<- AR.my\_time\_local  
now: Mon Feb 09 14:20:06 -0500 2009 = 1234207206.77585 \<- Time.now  
UTC: Mon Feb 09 19:20:06 UTC 2009 = 1234207206.0 \<- AR.my\_time.utc  
now: Mon Feb 09 19:20:06 UTC 2009 = 1234207206.77609 \<- Time.now.utc

default\_timezone = utc  
Local: Mon Feb 09 14:20:06 UTC 2009 = 1234189206.0 \<- AR.my\_time\_local  
now: Mon Feb 09 14:20:06 -0500 2009 = 1234207206.77807 \<- Time.now  
UTC: Mon Feb 09 19:20:06 UTC 2009 = 1234207206.0 \<- AR.my\_time.utc  
now: Mon Feb 09 19:20:06 UTC 2009 = 1234207206.7783 \<- Time.now.utc  
.

Is it possible to override AR default\_timezone values for testing?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [February 9, 2009, 10:53pm UTC](https://www.ruby-forum.com/t/ar-base-default-timezone/159889/2 "2009-02-09T22:53:48Z")

</div>

What you really want to do is mock the default\_timezone for the scope of  
a particular test. For example,

require ‘mocha’

class MagikTest \< ActiveSupport::TestCase  
def test\_something  
tz = ‘Alaska’  
ActiveRecord::Base.stubs(:default\_timezone).returns(tz)  
assert\_equal tz, ActiveRecord::Base.default\_timezone  
end  
end

This just shows you how the stubbing works; in practice, this test is  
testing the Mocha library, and not your app. 🙂 The trick here is to  
decide what you want the default\_timezone call to return, and assign  
that value to tz. Then every time you call AR::Base.default\_timezone  
during that test, you’ll get back the value of tz.

You’ll need to grab the mocha library first: gem install mocha

You can use this to test your app against multiple timezones to really  
make sure it works.

Brent

James B. wrote:

> I have a requirement to test code whose execution depends upon the  
> default\_timezone setting. I have tried several approaches to this but  
> nothing that I do in my test seems to alter the behaviour that is set in  
> environment.rb.
> 
> Is there a way to dynamically alter AR’s method of storing datetime  
> during a test? Currently I attempt to do this in this fashion:
> 
> ```
> ["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
> 
> ```
> 
> but the contents of the my\_time\_local and my\_time\_utc do not seem to be  
> what I expect:
> 
> default\_timezone = local  
> Local: Mon Feb 09 14:20:06 UTC 2009 = 1234189206.0 \<- AR.my\_time\_local  
> now: Mon Feb 09 14:20:06 -0500 2009 = 1234207206.77585 \<- Time.now  
> UTC: Mon Feb 09 19:20:06 UTC 2009 = 1234207206.0 \<- AR.my\_time.utc  
> now: Mon Feb 09 19:20:06 UTC 2009 = 1234207206.77609 \<- Time.now.utc
> 
> default\_timezone = utc  
> Local: Mon Feb 09 14:20:06 UTC 2009 = 1234189206.0 \<- AR.my\_time\_local  
> now: Mon Feb 09 14:20:06 -0500 2009 = 1234207206.77807 \<- Time.now  
> UTC: Mon Feb 09 19:20:06 UTC 2009 = 1234207206.0 \<- AR.my\_time.utc  
> now: Mon Feb 09 19:20:06 UTC 2009 = 1234207206.7783 \<- Time.now.utc  
> .
> 
> Is it possible to override AR default\_timezone values for testing?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [February 10, 2009, 12:53am UTC](https://www.ruby-forum.com/t/ar-base-default-timezone/159889/3 "2009-02-10T00:53:43Z")

</div>

in your test\_helper:

require ‘mocha’

Then, in your test

def test\_something  
tz = :local  
ActiveRecord::Base.expects(:default\_timezone).returns(tz)  
assert\_equal tz, ActiveRecord::Base.default\_timezone  
end

The trick is to set tz to be whatever you want to get back from  
AR::Base.default\_timezone. If you use AR::Base.expects, then your  
test will fail if AR::Base.default\_timezone is called more than once  
or not at all. If you don’t care how many times it gets called (i.e.,  
if that’s not something you need to test), then use “AR::Base.stubs  
(:default\_timezone).returns(tz)” instead.

Brent

On Feb 9, 11:30 am, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [February 10, 2009, 1:56am UTC](https://www.ruby-forum.com/t/ar-base-default-timezone/159889/4 "2009-02-10T01:56:47Z")

</div>

Brent M. wrote:

> in your test\_helper:
> 
> require ‘mocha’
> 
> Then, in your test
> 
> def test\_something  
> tz = :local  
> ActiveRecord::Base.expects(:default\_timezone).returns(tz)  
> assert\_equal tz, ActiveRecord::Base.default\_timezone  
> end
> 
> The trick is to set tz to be whatever you want to get back from  
> AR::Base.default\_timezone. If you use AR::Base.expects, then your  
> test will fail if AR::Base.default\_timezone is called more than once  
> or not at all. If you don’t care how many times it gets called (i.e.,  
> if that’s not something you need to test), then use “AR::Base.stubs  
> (:default\_timezone).returns(tz)” instead.

However, what I am testing is a library that actually modifies  
ActiveRecord behaviour based on the default\_timezone setting. So, it is  
pointless to mock or stub what I am actually testing. I eventually just  
overrode the environment setting in the test itself.

it “should set the UTC time (for \_at/\_on) correctly” do  
ActiveRecord::Base.default\_timezone = “utc”  
…  
end
