Created
May 4, 2010 17:04
-
-
Save kares/389660 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # Put this to test/time_zone_test_helper.rb and include the module into Your | |
| # base TestCase class e.g. test_helper.rb : | |
| # | |
| # require 'time_zone_test_helper' | |
| # | |
| # class ActiveSupport::TestCase | |
| # | |
| # include TimeZoneTestHelper | |
| # ... | |
| # | |
| # Now, in a time zone sensitive test case use the local time zone for tests : | |
| # | |
| # class MyTestCase < ActiveSupport::TestCase | |
| # | |
| # use_local_time_zone_for_tests | |
| # ... | |
| # | |
| # end | |
| # | |
| module TimeZoneTestHelper | |
| def self.included(base) | |
| base.extend ClassMethods | |
| end | |
| module ClassMethods | |
| def use_local_time_zone_for_tests | |
| self.send :setup, :setup_local_time_zone | |
| self.send :teardown, :reset_local_time_zone | |
| end | |
| end | |
| def setup_local_time_zone | |
| Time.zone = local_time_zone | |
| end | |
| def reset_local_time_zone | |
| Time.zone = nil | |
| end | |
| private | |
| def local_time_zone | |
| ActiveSupport::TimeZone[Time.now.utc_offset] | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment