<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SmartLogic Solutions Blog &#187; timecop</title>
	<atom:link href="http://blog.smartlogicsolutions.com/tag/timecop/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.smartlogicsolutions.com</link>
	<description>News and updates from the people at SmartLogic Solutions</description>
	<lastBuildDate>Tue, 30 Nov 2010 21:39:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Timecop 0.3.4 Released</title>
		<link>http://blog.smartlogicsolutions.com/2009/12/07/timecop-0-3-4-released/</link>
		<comments>http://blog.smartlogicsolutions.com/2009/12/07/timecop-0-3-4-released/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 21:39:06 +0000</pubDate>
		<dc:creator>John Trupiano</dc:creator>
				<category><![CDATA[John Trupiano]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[TATFT]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[rubygem]]></category>
		<category><![CDATA[tatft]]></category>
		<category><![CDATA[timecop]]></category>

		<guid isPermaLink="false">http://blog.smartlogicsolutions.com/?p=794</guid>
		<description><![CDATA[Timecop 0.3.4 has just been released. To install simply run: gem install timecop. Timecop is a RubyGem providing &#8220;time travel&#8221; and &#8220;time freezing&#8221; capabilities, making it dead simple to test time-dependent code. It provides a unified method to mock Time.now, Date.today, and DateTime.now in a single call. Documentation is hosted at RubyForge. The source code [...]]]></description>
			<content:encoded><![CDATA[<p>Timecop 0.3.4 has just been released.  To install simply run: <code>gem install timecop</code>.</p>
<p>Timecop is a RubyGem providing &#8220;time travel&#8221; and &#8220;time freezing&#8221; capabilities, making it dead simple to test time-dependent code.  It provides a unified method to mock Time.now, Date.today, and DateTime.now in a single call.</p>
<p>Documentation is hosted at <a href="http://johntrupiano.rubyforge.org/timecop/">RubyForge</a>.  The source code is hosted at <a href="http://github.com/jtrupiano/timecop">GitHub</a>.</p>
<p>Updates include:</p>
<p><span id="more-794"></span></p>
<p><strong>Maintenance</strong></p>
<ul>
<li>Fix various timezone-related issues.  Notably, when traveling to a DateTime<br />
    instance specified in a non-local timezone, convert provided DateTime<br />
    instance to a local instance and return that from DateTime.now.<br />
    Code contributed by Michaël Witrant [<a href="http://github.com/piglop">piglop</a>]
</li>
<li>Fix bug that would not allow Timecop to be used when Ruby&#8217;s &#8216;date&#8217;<br />
    library had not been previously loaded.<br />
    Code contributed by Tuomas Kareinen [<a href="http://github.com/tuomas">tuomas</a>]
</li>
<li>Fix bug when traveling to a DateTime across a DST boundary that<br />
    resulted in DateTime&#8217;s being off by an hour.
</li>
<li>Migrate argument parsing into Timecop::TimeStackItem to reduce the<br />
    responsibility of the Timecop class.
</li>
</ul>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://blog.smartlogicsolutions.com/2009/09/20/timecop-0-3-0-released/" rel="bookmark" class="crp_title">Timecop 0.3.0 Released</a></li><li><a href="http://blog.smartlogicsolutions.com/2008/12/24/timecop-2-released-freeze-and-rebase-time-ruby/" rel="bookmark" class="crp_title">Timecop 0.2.0 Released: Freeze and Rebase Time in Ruby</a></li><li><a href="http://blog.smartlogicsolutions.com/2008/11/19/timecop-freeze-time-in-ruby-for-better-testing/" rel="bookmark" class="crp_title">Timecop: Freeze Time in Ruby for Better Testing</a></li><li><a href="http://blog.smartlogicsolutions.com/2010/05/13/rack-rewrite-1-0-0-released/" rel="bookmark" class="crp_title">Rack::Rewrite 1.0.0 Released</a></li><li><a href="http://blog.smartlogicsolutions.com/2010/01/06/rack-rewrite-0-2-1-released/" rel="bookmark" class="crp_title">Rack::Rewrite 0.2.1 Released</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://blog.smartlogicsolutions.com/2009/12/07/timecop-0-3-4-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Timecop 0.3.0 Released</title>
		<link>http://blog.smartlogicsolutions.com/2009/09/20/timecop-0-3-0-released/</link>
		<comments>http://blog.smartlogicsolutions.com/2009/09/20/timecop-0-3-0-released/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 03:27:34 +0000</pubDate>
		<dc:creator>John Trupiano</dc:creator>
				<category><![CDATA[John Trupiano]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[TATFT]]></category>
		<category><![CDATA[rubygem]]></category>
		<category><![CDATA[tatft]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[timecop]]></category>

		<guid isPermaLink="false">http://blog.smartlogicsolutions.com/?p=704</guid>
		<description><![CDATA[Timecop 0.3.0 has just been released. To install simply run: gem install timecop. Timecop is a RubyGem providing &#8220;time travel&#8221; and &#8220;time freezing&#8221; capabilities, making it dead simple to test time-dependent code. It provides a unified method to mock Time.now, Date.today, and DateTime.now in a single call. Documentation is on RubyForge. The source code is [...]]]></description>
			<content:encoded><![CDATA[<p>Timecop 0.3.0 has just been released.  To install simply run: <code>gem install timecop</code>.</p>
<p>Timecop is a RubyGem providing &#8220;time travel&#8221; and &#8220;time freezing&#8221; capabilities, making it dead simple to test time-dependent code.  It provides a unified method to mock Time.now, Date.today, and DateTime.now in a single call.</p>
<p>Documentation is on <a href="http://johntrupiano.rubyforge.org/timecop/">RubyForge</a>.  The source code is hosted at <a href="http://github.com/jtrupiano/timecop">GitHub</a>.</p>
<p>Updates include:</p>
<p><strong>API</strong></p>
<ul>
<li>Completely remove Timecop#unset_all (deprecated by Timecop#return in 0.2.0)</li>
<li>Return Time.now from #freeze, #travel and #return  &#8212; code contributed by Keith Bennett (<a href="http://github.com/keithrbennett">keithrbennett</a>)</li>
</ul>
<p><strong>Maintenance</strong></p>
<ul>
<li>Fix bug that left Time#mock_time set in some instances</li>
<li>Upped build dependency to jeweler ~> 1.2.1</li>
<li>Don&#8217;t pollute top-level namespace with classes/constants</li>
</ul>
<p><strong>Documentation</strong></p>
<ul>
<li>Clearer examples in the README, better description in the gemspec</li>
<li>Improve RDoc</li>
</ul>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://blog.smartlogicsolutions.com/2009/12/07/timecop-0-3-4-released/" rel="bookmark" class="crp_title">Timecop 0.3.4 Released</a></li><li><a href="http://blog.smartlogicsolutions.com/2008/12/24/timecop-2-released-freeze-and-rebase-time-ruby/" rel="bookmark" class="crp_title">Timecop 0.2.0 Released: Freeze and Rebase Time in Ruby</a></li><li><a href="http://blog.smartlogicsolutions.com/2008/11/19/timecop-freeze-time-in-ruby-for-better-testing/" rel="bookmark" class="crp_title">Timecop: Freeze Time in Ruby for Better Testing</a></li><li><a href="http://blog.smartlogicsolutions.com/2010/01/06/rack-rewrite-0-2-1-released/" rel="bookmark" class="crp_title">Rack::Rewrite 0.2.1 Released</a></li><li><a href="http://blog.smartlogicsolutions.com/2010/05/13/rack-rewrite-1-0-0-released/" rel="bookmark" class="crp_title">Rack::Rewrite 1.0.0 Released</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://blog.smartlogicsolutions.com/2009/09/20/timecop-0-3-0-released/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Timecop 0.2.0 Released: Freeze and Rebase Time in Ruby</title>
		<link>http://blog.smartlogicsolutions.com/2008/12/24/timecop-2-released-freeze-and-rebase-time-ruby/</link>
		<comments>http://blog.smartlogicsolutions.com/2008/12/24/timecop-2-released-freeze-and-rebase-time-ruby/#comments</comments>
		<pubDate>Wed, 24 Dec 2008 06:14:35 +0000</pubDate>
		<dc:creator>John Trupiano</dc:creator>
				<category><![CDATA[John Trupiano]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[TATFT]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[jtrupiano]]></category>
		<category><![CDATA[rubygem]]></category>
		<category><![CDATA[tatft]]></category>
		<category><![CDATA[test]]></category>
		<category><![CDATA[timecop]]></category>

		<guid isPermaLink="false">http://blog.smartlogicsolutions.com/?p=445</guid>
		<description><![CDATA[I just released version 0.2.0 of Timecop this evening (morning). The primary feature added was the distinction between &#8220;freezing&#8221; and &#8220;rebasing&#8221; time. In 0.1.0, Timecop.travel would actually freeze time. This is no longer the case. Rather, a time offset will be calculated, and a running clock is simulated by always offsetting the time returned by [...]]]></description>
			<content:encoded><![CDATA[<p>I just released version 0.2.0 of <a href="http://github.com/jtrupiano/timecop">Timecop</a> this evening (morning).</p>
<p>The primary feature added was the distinction between &#8220;freezing&#8221; and &#8220;rebasing&#8221; time.  In 0.1.0, <code>Timecop.travel</code> would actually freeze time.  This is no longer the case.  Rather, a time offset will be calculated, and a running clock is simulated by always offsetting the time returned by Time.now (and friends) by the original offset.</p>
<p>(Note that time can still be frozen with <code>Timecop.freeze</code>.)</p>
<p><span id="more-445"></span></p>
<p>To update a previous version:</p>
<pre><code>sudo gem update timecop</code></pre>
<p>To install for the first time: </p>
<pre><code>sudo gem install timecop</code></pre>
<p>The new feature set allows us to avoid the following common pitfalls that were present in 0.1.0. </p>
<ol>
<li>In rails, if you had set up a time mock in the development environment, then every time you invoked <code>script/generate migration</code> (and others), the generator would read your mocked instance of Time.now, and use that date for the migration name.</li>
<li>Any benchmarking/timing code executed within the yielded block would always return times of 0 seconds.  This was due to time being frozen.</li>
</ol>
<p>The other major change to the API was the deprecating of Timecop.unset_all in favor or Timecop.return.</p>
<p>This update also vastly <a href="http://johntrupiano.rubyforge.org/timecop">improves the rdoc</a>, making the public API much more accessible.  The Timecop class has particularly thorough documentation.</p>
<p>For a more complete description of the gem, please refer to the <a href="http://blog.smartlogicsolutions.com/2008/11/19/timecop-freeze-time-in-ruby-for-better-testing/">introductory blog post</a>.</p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://blog.smartlogicsolutions.com/2009/09/20/timecop-0-3-0-released/" rel="bookmark" class="crp_title">Timecop 0.3.0 Released</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/12/07/timecop-0-3-4-released/" rel="bookmark" class="crp_title">Timecop 0.3.4 Released</a></li><li><a href="http://blog.smartlogicsolutions.com/2008/11/19/timecop-freeze-time-in-ruby-for-better-testing/" rel="bookmark" class="crp_title">Timecop: Freeze Time in Ruby for Better Testing</a></li><li><a href="http://blog.smartlogicsolutions.com/2010/05/13/directory-conventions-for-rack-middleware-rubygems/" rel="bookmark" class="crp_title">Directory Conventions for Rack Middleware RubyGems</a></li><li><a href="http://blog.smartlogicsolutions.com/2008/12/18/puremvcgen-011-released-now-works-on-windows/" rel="bookmark" class="crp_title">PureMVCGen 0.1.1 Released &#8211; Now Works on Windows</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://blog.smartlogicsolutions.com/2008/12/24/timecop-2-released-freeze-and-rebase-time-ruby/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Timecop: Freeze Time in Ruby for Better Testing</title>
		<link>http://blog.smartlogicsolutions.com/2008/11/19/timecop-freeze-time-in-ruby-for-better-testing/</link>
		<comments>http://blog.smartlogicsolutions.com/2008/11/19/timecop-freeze-time-in-ruby-for-better-testing/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 15:07:44 +0000</pubDate>
		<dc:creator>John Trupiano</dc:creator>
				<category><![CDATA[John Trupiano]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[tatft]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[timecop]]></category>

		<guid isPermaLink="false">http://blog.smartlogicsolutions.com/?p=360</guid>
		<description><![CDATA[The API mentioned in this blog post is specific to v0.1.0. The latest version is v0.2.0. See the blog post announcement, the documentation, or the github home page. This past weekend I released v0.1.0 of the Timecop gem. Timecop makes it dead simple to travel through or freeze time for the sake of creating a [...]]]></description>
			<content:encoded><![CDATA[<div style="background-color: #e0e0e0; border-color: #999999; padding: 6px;">
The API mentioned in this blog post is specific to v0.1.0.  The latest version is v0.2.0.  See the <a href="http://blog.smartlogicsolutions.com/2008/12/24/timecop-2-released-freeze-and-rebase-time-ruby/">blog post announcement</a>, the <a href="http://johntrupiano.rubyforge.org/timecop">documentation</a>, or the <a href="http://github.com/jtrupiano/timecop">github home page</a>.
</div>
<p>This past weekend I released v0.1.0 of the <a href="http://github.com/jtrupiano/timecop">Timecop</a> gem.  Timecop makes it dead simple to travel through or freeze time for the sake of creating a predictable and ultimately testable scenario.</p>
<p>The gem is derived from a plugin I wrote a while back to achieve more or less the same functionality for an extremely time-sensitive application.  My goals for the gem included:</p>
<ol>
<li><strong>Drop-in-ability</strong>: The primary goal is to allow your app to continue to use Time.now, Date.today and DateTime.now as normal within your application.  No overloading of functions with optional arguments (a la today=Date.today) just so you can write test cases.</li>
<li><strong>Environment independence</strong>: I wanted the gem to work (a) w/ rails (ActiveSupport actually), (b) w/ plain ruby when the &#8216;date&#8217; library has been loaded, and (c) w/ plain ruby when the &#8216;date&#8217; library had not been loaded.</li>
<li><strong>Library independence</strong>: I could have utilized mocha to achieve the mocking functionality found under the hood, but because I wanted this to work with plain vanilla ruby, libraries like mocha are out.</li>
<li><strong>Short-term time travel</strong>: I wanted to expose the ability to temporarily change the concept of &#8220;now.&#8221;  This is particularly helpful when writing tests where time needs to pass.</li>
<li><strong>Long-range time travel</strong>: I wanted to expose the ability to change the concept of &#8220;now&#8221; for an indeterminate period of time.  This is particularly helpful when setting up a rails test environment along with the test data.</li>
<li><strong>Nested time travel</strong>: I wanted to provide the ability to nest traveling, allowing the state to be kept within each block (we&#8217;ll see an example later).</li>
</ol>
<p>The gem is hosted on RubyForge and can be installed by simply running:</p>
<pre>sudo gem install timecop</pre>
<p><span id="more-360"></span></p>
<h2>Using Timecop</h2>
<p>The most basic example is to utilize this within a test.  Consider the following test case:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'timecop'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'test/unit'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> MyTestCase <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">Test::Unit::TestCase</span></span>
  <span style="color:#9966CC; font-weight:bold;">def</span> test_mortgage_due_in_30_days
    john = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    john.<span style="color:#9900CC;">sign_mortgage</span>!
    assert !john.<span style="color:#9900CC;">mortgage_payment_due</span>?
    Timecop.<span style="color:#9900CC;">travel</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">30</span>.<span style="color:#9900CC;">days</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      assert john.<span style="color:#9900CC;">mortgage_payment_due</span>?
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>What&#8217;s nice about this API is that the #travel function will take several different object types.  You can pass a Time instance, a Date instance, a DateTime instance, or individual time parameters (the same arguments Time.local()) takes.  Also, if you&#8217;re a big fan of the <a href="http://chronic.rubyforge.org/">chronic</a> gem, then you can easily combine these two:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Timecop.<span style="color:#9900CC;">travel</span><span style="color:#006600; font-weight:bold;">&#40;</span>Chronic.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'this tuesday 5:00'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#008000; font-style:italic;"># test-fu</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>For some applications, you may find it useful to completely re-base the concept of &#8220;now&#8221; for your test environment.  Whenever time-sensitivity is a major priority for your application (e.g. an ordering and delivery system, or any system that encapsulates the concept of &#8220;availability&#8221; at a particularly fine-grained level) , you may find it useful to &#8220;root&#8221; your test data at a particular point in time, allowing you to write very specific tests that don&#8217;t fall victim to the unstoppable march of real time.</p>
<p>To achieve a static date for your entire test environment, you can simply drop the following into config/environments/test.rb (or config/test/environment.rb if you use <a href="http://github.com/jtrupiano/environmentalist">environmentalist</a>):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Setting &quot;now&quot; to May 15, 2008 10:00:00 AM</span>
Timecop.<span style="color:#9900CC;">travel</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">local</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2008</span>, <span style="color:#006666;">5</span>, <span style="color:#006666;">15</span>, <span style="color:#006666;">10</span>, <span style="color:#006666;">0</span>, <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>This will cause your whole application to run at a static time.  As mentioned previously, this is particularly important for creating static test cases.  You may think that you can achieve this by always using relative times, but this proves much more difficult than initially perceived.  For example, let&#8217;s consider the following scenario, which is an extension of the mortgage example beforehand.  Let&#8217;s say the business rule is that a mortgage payment is due every month on the same date as the lease is signed, except for when that day falls on a weekend, in which case the payment would be due the following Monday.  As such, it&#8217;s extremely crucial for us to write a test case where we know the date in the next month falls on a weekend, and a separate test case where the date in the next month falls on a weekday.  As such, there are certain months where the following test (not using Timecop) would fail:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'test/unit'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> MyTestCase <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">Test::Unit::TestCase</span></span>
  <span style="color:#9966CC; font-weight:bold;">def</span> test_mortgage_due_in_30_days
    john = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    john.<span style="color:#9900CC;">sign_mortgage</span>!
    assert !john.<span style="color:#9900CC;">mortgage_payment_due</span>?
    assert john.<span style="color:#9900CC;">mortgage_payment_due</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>Chronic.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;1 month from now&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Furthermore, you&#8217;ll notice I had to pass a date to the mortgage_payment_due? function, essentially requiring me to change the signature of that function to take an optional date argument.  Basically, in order to test my application, I&#8217;ve had to go ahead and change my actual code!  Something clearly smells here.  Another option would be to use mocha to mock Time.now.  However, mocha has its own limitations with its inability to unmock.  Lastly, specifically mocking Time.now would leave us high and dry if our implementation decided to start using DateTime.now for comparison in lieu of Time.now.</p>
<p>Another feature that I want to illustrate is the ability to nest #travel commands.  The following assertions will all pass:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">   <span style="color:#008000; font-style:italic;"># this test is taken directly from the test/test_timecop.rb</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> test_recursive_travel
    t = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">local</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2008</span>, <span style="color:#006666;">10</span>, <span style="color:#006666;">10</span>, <span style="color:#006666;">10</span>, <span style="color:#006666;">10</span>, <span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    Timecop.<span style="color:#9900CC;">travel</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2008</span>, <span style="color:#006666;">10</span>, <span style="color:#006666;">10</span>, <span style="color:#006666;">10</span>, <span style="color:#006666;">10</span>, <span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> 
      assert_equal t, <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
      t2 = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">local</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2008</span>, <span style="color:#006666;">9</span>, <span style="color:#006666;">9</span>, <span style="color:#006666;">9</span>, <span style="color:#006666;">9</span>, <span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      Timecop.<span style="color:#9900CC;">travel</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2008</span>, <span style="color:#006666;">9</span>, <span style="color:#006666;">9</span>, <span style="color:#006666;">9</span>, <span style="color:#006666;">9</span>, <span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        assert_equal t2, <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      assert_equal t, <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    assert_nil <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:mock_time</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Lastly, Timecop handles the scenario where exceptions are raised within the blocks passed to the #travel function.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;"># this test is taken directly from the test/test_timecop.rb</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> test_exception_thrown_in_travel_block_properly_resets_time
    t = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">local</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2008</span>, <span style="color:#006666;">10</span>, <span style="color:#006666;">10</span>, <span style="color:#006666;">10</span>, <span style="color:#006666;">10</span>, <span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      Timecop.<span style="color:#9900CC;">travel</span><span style="color:#006600; font-weight:bold;">&#40;</span>t<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        assert_equal t, <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
        <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#996600;">&quot;blah exception&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span>
      assert_not_equal t, <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
      assert_nil <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:mock_time</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Timecop is useful both in small and large consumption.  Just as mocha can be helpful to give you a quick mock here and there, Timecop can help guide you very easily through testing the few time-sensitive parts of your application.  And for those unique applications where everything is time-sensitive, Timecop gives you a very simple way to stabilize your concept of now.</p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://blog.smartlogicsolutions.com/2009/09/20/timecop-0-3-0-released/" rel="bookmark" class="crp_title">Timecop 0.3.0 Released</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/12/07/timecop-0-3-4-released/" rel="bookmark" class="crp_title">Timecop 0.3.4 Released</a></li><li><a href="http://blog.smartlogicsolutions.com/2008/12/24/timecop-2-released-freeze-and-rebase-time-ruby/" rel="bookmark" class="crp_title">Timecop 0.2.0 Released: Freeze and Rebase Time in Ruby</a></li><li><a href="http://blog.smartlogicsolutions.com/2010/10/08/testing-ajax-with-testunit/" rel="bookmark" class="crp_title">Testing AJAX with Test::Unit</a></li><li><a href="http://blog.smartlogicsolutions.com/2008/07/07/i-cant-upgrade-rubygems-from-111-to-120-on-ubuntu/" rel="bookmark" class="crp_title">I can&#8217;t upgrade RubyGems from 1.1.1 to 1.2.0 on Ubuntu</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://blog.smartlogicsolutions.com/2008/11/19/timecop-freeze-time-in-ruby-for-better-testing/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

