<?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>One Man's Walk in work &#187; mocha</title>
	<atom:link href="http://onemanswalk.com/work/tag/mocha/feed/" rel="self" type="application/rss+xml" />
	<link>http://onemanswalk.com/work</link>
	<description>jeremy lightsmith on agile, ruby, and consulting</description>
	<lastBuildDate>Wed, 08 Sep 2010 05:57:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>More Fun with Times, Mocks, and Closures</title>
		<link>http://onemanswalk.com/work/2007/05/24/more-fun-with-times-mocks-and-closures/</link>
		<comments>http://onemanswalk.com/work/2007/05/24/more-fun-with-times-mocks-and-closures/#comments</comments>
		<pubDate>Thu, 24 May 2007 05:48:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[mocha]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	I solved a complex problem in cruise today with some non-trivial mocking.  Check this one out :


	I needed to test that every x amount of time, we do a clean checkout.  It can be every 6 hours, 2 days, whatever.  How do you test this?

	Well, maybe you could mock the time.

	
Time.stubs&#40;:now&#41;.returns&#40;Time.now + [...]]]></description>
			<content:encoded><![CDATA[	<p>I solved a complex problem in cruise today with some non-trivial mocking.  Check this one out :</p>


	<p>I needed to test that every x amount of time, we do a clean checkout.  It can be every 6 hours, 2 days, whatever.  How do you test this?</p>

	<p>Well, maybe you could mock the time.</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">stubs</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:now</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">returns</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;">2</span>.<span style="color:#9900CC;">hours</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>
</p>



	<p>That almost works, except that the code works by touching a file.  And touch doesn&#8217;t use <code>Time.now</code>.  Now at this point, we could go crazy, and mock <code>FileUtils.touch</code>, of course that means we&#8217;ll also have to mock <code>File.exists?</code> and then what are we really testing?</p>

	<p>Instead, I used Mocha to temporarily replace <code>FileUtils.touch</code> with my own implementation that acts like the original but uses my own value of time.  It looks like this so far.</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  marker = sandbox.<span style="color:#9900CC;">root</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'/last_clean_checkout_timestamp'</span>
&nbsp;
  now = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
  <span style="color:#CC00FF; font-weight:bold;">FileUtils</span>.<span style="color:#9900CC;">stubs</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:touch</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">with</span><span style="color:#006600; font-weight:bold;">&#40;</span>marker<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">returns</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">proc</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>marker, <span style="color:#996600;">'w'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> f</pre></div></div>
</p>



	<p>you&#8217;ll notice that both of these stubs are returning procs that reference <code>now</code> a local variable&#8230;.</p>

	<p>That&#8217;s ruby magic.</p>

	<p>It means that I can now forget about mocks and write the rest of my test like this :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#0066ff; font-weight:bold;">@project</span>.<span style="color:#9900CC;">do_clean_checkout</span> <span style="color:#ff3333; font-weight:bold;">:every</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span>.<span style="color:#9900CC;">hour</span>
&nbsp;
  assert <span style="color:#0066ff; font-weight:bold;">@project</span>.<span style="color:#9900CC;">do_clean_checkout</span>?
  assert !@project.<span style="color:#9900CC;">do_clean_checkout</span>?
&nbsp;
  now <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">59</span>.<span style="color:#9900CC;">minutes</span>
  assert !@project.<span style="color:#9900CC;">do_clean_checkout</span>?
&nbsp;
  now <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">2</span>.<span style="color:#9900CC;">minutes</span>
  assert <span style="color:#0066ff; font-weight:bold;">@project</span>.<span style="color:#9900CC;">do_clean_checkout</span>?
  assert !@project.<span style="color:#9900CC;">do_clean_checkout</span>?
&nbsp;
  <span style="color:#0066ff; font-weight:bold;">@project</span>.<span style="color:#9900CC;">do_clean_checkout</span> <span style="color:#ff3333; font-weight:bold;">:every</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2</span>.<span style="color:#9900CC;">days</span>
  now <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>.<span style="color:#9900CC;">day</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">23</span>.<span style="color:#9900CC;">hours</span>
  assert !@project.<span style="color:#9900CC;">do_clean_checkout</span>?
&nbsp;
  now <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">2</span>.<span style="color:#9900CC;">hours</span>
  assert <span style="color:#0066ff; font-weight:bold;">@project</span>.<span style="color:#9900CC;">do_clean_checkout</span>?</pre></div></div>
</p>



	<p>Instead of changing the mocks several times in between each test, I can just change my local variable <code>now</code>.  Because of closures, the mocked out methods return the new value.</p>

	<p>Ruby is awesome (and Mocha is pretty sweet too)</p>
 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2007/05/24/more-fun-with-times-mocks-and-closures/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
