<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: ruby code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 28 Aug 2008 20:31:03 GMT</pubDate>
    <description>DZone Snippets: ruby code</description>
    <item>
      <title>Using time travel to unit test time-based data</title>
      <link>http://snippets.dzone.com/posts/show/1706</link>
      <description>Searching for a way to test time-based data in ruby, I came across an excellent suggestion from Rick Olson (technoweenie).  I've cleaned it a little to make it clearer to read, but the main credit should go to Rick.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class &lt;&lt; Time&lt;br /&gt;  unless method_defined? :now_before_time_travel&lt;br /&gt;    alias_method :now_before_time_travel, :now&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def now &lt;br /&gt;    @now || now_before_time_travel &lt;br /&gt;  end &lt;br /&gt;  &lt;br /&gt;  def travel_to(time, &amp;block)&lt;br /&gt;    @now = time&lt;br /&gt;    block.call&lt;br /&gt;  ensure&lt;br /&gt;    @now = nil&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def test_something_involving_time&lt;br /&gt;  account = Time.travel_to(creation_time = Time.now) do&lt;br /&gt;    Account.new  &lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  assert_equal creation_time, account.creation_time&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 16 Mar 2006 18:25:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1706</guid>
      <author>tomafro (Tom Ward)</author>
    </item>
  </channel>
</rss>
