<?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; testing</title>
	<atom:link href="http://onemanswalk.com/work/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://onemanswalk.com/work</link>
	<description>jeremy lightsmith on agile, ruby, and consulting</description>
	<lastBuildDate>Wed, 04 May 2011 04:29:54 +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>Eventual Consistency, or things will all work out&#8230;eventually</title>
		<link>http://onemanswalk.com/work/2008/08/05/eventual-consistency-or-things-will-all-work-out-eventually/</link>
		<comments>http://onemanswalk.com/work/2008/08/05/eventual-consistency-or-things-will-all-work-out-eventually/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 00:12:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dsls]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	So we&#8217;re working with Amazon&#8217;s SimpleDB.  It&#8217;s pretty sweet, though the ruby libraries for it are still a bit primitive.  One of the problems you run up against when you&#8217;re writing integration tests against it is eventual consistency.

	Take this test :

	
it &#34;should save&#34; do
  customer = Customer.create!&#40;:name =&#62; 'bob', :email =&#62; 'bob@example.com'&#41;
 [...]]]></description>
			<content:encoded><![CDATA[	<p>So we&#8217;re working with <a href="http://www.amazon.com/SimpleDB-AWS-Service-Pricing/b?ie=UTF8&#038;node=342335011" title="">Amazon&#8217;s SimpleDB</a>.  It&#8217;s pretty sweet, though the ruby libraries for it are still a bit primitive.  One of the problems you run up against when you&#8217;re writing integration tests against it is eventual consistency.</p>

	<p>Take this test :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">it <span style="color:#996600;">&quot;should save&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  customer = Customer.<span style="color:#9900CC;">create</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'bob'</span>, <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'bob@example.com'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  customer = Customer.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>customer.<span style="color:#9900CC;">key</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  customer.<span style="color:#9900CC;">name</span>.<span style="color:#9900CC;">should</span> == <span style="color:#996600;">'bob'</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>The way SimpleDB works, you&#8217;re assured that Customer.find will work&#8230;eventually, but not right away.</p>

	<p>For a couple days we contented ourselves to just run the integration tests a couple times until they didn&#8217;t error out.  But that got old.</p>

	<p>Enter &#8220;eventually&#8221; :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">it <span style="color:#996600;">&quot;should save&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  customer = Customer.<span style="color:#9900CC;">create</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'bob'</span>, <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'bob@example.com'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  customer = eventually <span style="color:#006600; font-weight:bold;">&#123;</span> Customer.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>customer.<span style="color:#9900CC;">key</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  customer.<span style="color:#9900CC;">name</span>.<span style="color:#9900CC;">should</span> == <span style="color:#996600;">'bob'</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>It&#8217;s a very simple method (below) that just retries the passed in block until it succeeds, timing out after 10 tries.  Super simple, works like a charm.  Thank you ruby.</p>

	<p>Here&#8217;s the source :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">def</span> eventually<span style="color:#006600; font-weight:bold;">&#40;</span>tries = <span style="color:#006666;">0</span>, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">yield</span>
<span style="color:#9966CC; font-weight:bold;">rescue</span>
  <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#9966CC; font-weight:bold;">if</span> tries <span style="color:#006600; font-weight:bold;">&gt;</span>= <span style="color:#006666;">10</span>
  <span style="color:#CC0066; font-weight:bold;">sleep</span> <span style="color:#006666;">0.5</span>
  eventually<span style="color:#006600; font-weight:bold;">&#40;</span>tries <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span>, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>I will say, I can&#8217;t help but smile every time I write &#8220;eventually&#8221; in a test&#8230; <img src='http://onemanswalk.com/work/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2008/08/05/eventual-consistency-or-things-will-all-work-out-eventually/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PhpUnit for TextMate</title>
		<link>http://onemanswalk.com/work/2008/07/18/phpunit-for-textmate/</link>
		<comments>http://onemanswalk.com/work/2008/07/18/phpunit-for-textmate/#comments</comments>
		<pubDate>Fri, 18 Jul 2008 23:55:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[textmate]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	Who would have thought I&#8217;d be doing php?

	Anyway, yesterday I found myself doing php and really wanting it to hurt less.  I downloaded a textmate bundle for phpunit from github.  It wasn&#8217;t quite what I wanted, so I forked it and hacked it, and made it work a bit more like the rspec [...]]]></description>
			<content:encoded><![CDATA[	<p>Who would have thought I&#8217;d be doing php?</p>

	<p>Anyway, yesterday I found myself doing php and really wanting it to hurt less.  I downloaded a <a href="http://github.com/meritt/phpunit-tmbundle" title="">textmate bundle for phpunit</a> from github.  It wasn&#8217;t quite what I wanted, so I forked it and hacked it, and made it work a bit more like the rspec bundle.</p>

	<p>It now has:</p>

	<p>Commands</p>
	<ul>
		<li>run tests (in current file)</li>
		<li>run single test</li>
	</ul>

	<p>Snippets</p>
	<ul>
		<li>test</li>
	</ul>

	<p>And much to my surprise, it was way easy to do.  All in ruby, all with specs testing it.</p>

	<p>This article was going to be telling all y&#8217;all to come over to <em>my</em> cool fork of phpunit, but I sent the original author a pull request yesterday, and I just looked, and he&#8217;s pulled all my changes.  Rock!</p>

	<p>So go forth and enjoy the goodness that an extensible editor like textmate, oss code like the phpunit bundle, and github can bring about.</p>

	<p><a href="http://github.com/meritt/phpunit-tmbundle" title=""><span class="caps">PHP</span>Unit.bundle for textmate</a></p>

 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2008/07/18/phpunit-for-textmate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clickable Stack Traces on your Rails Error Page</title>
		<link>http://onemanswalk.com/work/2008/04/30/clickable-stack-traces-on-your-rails-error-page/</link>
		<comments>http://onemanswalk.com/work/2008/04/30/clickable-stack-traces-on-your-rails-error-page/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 18:29:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	Wouldn&#8217;t it be nice if when an error happened in your application, you could not only see the stack trace, but click on a line and jump to the offending code?  This is not groundbreaking stuff, I know, I had this like 10 years ago in C++ and later Java fat clients, and I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[	<p>Wouldn&#8217;t it be nice if when an error happened in your application, you could not only see the stack trace, but click on a line and jump to the offending code?  This is not groundbreaking stuff, I know, I had this like 10 years ago in C++ and later Java fat clients, and I&#8217;m sure other languages &#038; IDEs had it too &#8211; but somehow in moving to writing web apps in Ruby, I lost it.</p>

	<p>I want it back damnit!</p>

	<p>Turns out it&#8217;s pretty easy to get back (at least it is if you use textmate) &#8211; check it out.</p>

	<h2>Custom Error Page</h2>

	<p>First, to get a custom error page for your project, add something like this to your application.rb :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">def</span> rescue_action_locally<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
  render <span style="color:#ff3333; font-weight:bold;">:template</span>  <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;application/public_error&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:layout</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">alias</span> rescue_action_in_public render_action_locally</pre></div></div>
</p>



	<p>Note, if you&#8217;re using exception notifiable, you probably want to change the last line to something like :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">alias</span> render_404 rescue_action_locally
<span style="color:#9966CC; font-weight:bold;">alias</span> render_500 rescue_action_locally</pre></div></div>
</p>



	<p>We use markaby, so our public_error template looks something like this; it&#8217;s probably a good idea to keep this simple and not use a layout, just in case the error came from the layout :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">html <span style="color:#9966CC; font-weight:bold;">do</span>
  head <span style="color:#9966CC; font-weight:bold;">do</span>
    title action_name
    stylesheet_link_tag <span style="color:#996600;">'error'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  body <span style="color:#9966CC; font-weight:bold;">do</span>
    div.<span style="color:#9900CC;">error</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      div.<span style="color:#9900CC;">message</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        h1 <span style="color:#996600;">&quot;Whoops&quot;</span>
&nbsp;
        p <span style="color:#996600;">&quot;We detected an error.  Don't worry, though, 
we've been notified and we're on it.&quot;</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>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>




	<h2>Adding a Stack Trace w/ Links to the Error Page</h2>

	<p>So, it would be helpful to us for our error page to tell us more in our development and staging environments.  We do use exception notifiable, so we don&#8217;t actually need or want it to say anything else to a real user in production.  Adding this to our template, it now looks like this :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">html <span style="color:#9966CC; font-weight:bold;">do</span>
  head <span style="color:#9966CC; font-weight:bold;">do</span>
    title action_name
    stylesheet_link_tag <span style="color:#996600;">'error'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  body <span style="color:#9966CC; font-weight:bold;">do</span>
    div.<span style="color:#9900CC;">error</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      div.<span style="color:#9900CC;">message</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        h1 <span style="color:#996600;">&quot;Whoops&quot;</span>
&nbsp;
        p <span style="color:#996600;">&quot;We detected an error.  Don't worry, though, 
we've been notified and we're on it.&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">if</span> RAILS_ENV != <span style="color:#996600;">'production'</span>
        div.<span style="color:#9900CC;">stack_trace</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          h2 <span style="color:#996600;">&quot;Stack Trace&quot;</span>
          div <span style="color:#006600; font-weight:bold;">&#123;</span> link_to_code $!.<span style="color:#9900CC;">to_s</span>.<span style="color:#9900CC;">to_s</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span>, <span style="color:#996600;">&quot;
&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
          hr
          div <span style="color:#006600; font-weight:bold;">&#123;</span> link_to_code $!.<span style="color:#9900CC;">backtrace</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;
&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</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>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>What&#8217;s that &#8220;link_to_code&#8221; method in there?</p>

	<p>It&#8217;s a method in application_helper that replaces any path with a textmate url to open up that file on your local system and jump to the offending line.  Check it out :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">def</span> link_to_code<span style="color:#006600; font-weight:bold;">&#40;</span>text<span style="color:#006600; font-weight:bold;">&#41;</span>
  text.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>\w\.<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">*</span>\<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#91;</span>\w\<span style="color:#006600; font-weight:bold;">/</span>\.<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>\:<span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>match<span style="color:#006600; font-weight:bold;">|</span>
    file = $<span style="color:#006666;">1</span>.<span style="color:#9900CC;">starts_with</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;/&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? $<span style="color:#006666;">1</span> : <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>RAILS_ROOT, $<span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    link_to match, <span style="color:#996600;">&quot;txmt://open?url=file://#{file}&amp;line=#{$2}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>That&#8217;s it.  Suddenly, stack traces are friendly again!</p>

 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2008/04/30/clickable-stack-traces-on-your-rails-error-page/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Customizing Markaby &#8211; Language Level Refactorings</title>
		<link>http://onemanswalk.com/work/2007/09/26/customizing-markaby-language-level-refactorings/</link>
		<comments>http://onemanswalk.com/work/2007/09/26/customizing-markaby-language-level-refactorings/#comments</comments>
		<pubDate>Wed, 26 Sep 2007 23:14:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dsls]]></category>
		<category><![CDATA[markaby]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	It&#8217;s very easy to call out to methods in markaby, but it&#8217;d be nice if you could actually customize the dsl as well.

	For example, on many of our pages we have a bottom row that has buttons that look a certain way.  So on every page, we have :

	
  table&#40;:width =&#62; &#34;100%&#34;&#41; do
 [...]]]></description>
			<content:encoded><![CDATA[	<p>It&#8217;s very easy to call out to methods in markaby, but it&#8217;d be nice if you could actually customize the dsl as well.</p>

	<p>For example, on many of our pages we have a bottom row that has buttons that look a certain way.  So on every page, we have :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  table<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:width</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;100%&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    tr <span style="color:#9966CC; font-weight:bold;">do</span>
      td.<span style="color:#9900CC;">left</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        previous_button
        first_button
      <span style="color:#9966CC; font-weight:bold;">end</span>
      td.<span style="color:#9900CC;">center</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        <span style="color:#CC0066; font-weight:bold;">print</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      td.<span style="color:#9900CC;">right</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        next_button
        last_button
      <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>



	<p>The code for the actual buttons changes, and after a few tries to extract the whole thing into a single method, we gave up.  Our efforts had made it harder to read, not easier.  There was always just a little too much variance, and it didn&#8217;t feel right.</p>

	<p>What we really wanted to write was :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  last_row <span style="color:#9966CC; font-weight:bold;">do</span>
    column <span style="color:#9966CC; font-weight:bold;">do</span>
      previous_button
      first_button
    <span style="color:#9966CC; font-weight:bold;">end</span>
    column <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#CC0066; font-weight:bold;">print</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    column <span style="color:#9966CC; font-weight:bold;">do</span>
      next_button
      last_button
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>This lets the buttons that change all stay in the view, and gets rid of the skeleton and positional stuff that doesn&#8217;t change.  Furthermore, it&#8217;s <span class="caps">DRY</span> and puts all that positional logic in one place instead of scattered across 20 views.</p>

	<p>How to do this?</p>

	<p>I wrote a test (in RSpec) that looks something like :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">describe ApplicationHelper <span style="color:#9966CC; font-weight:bold;">do</span>
  it <span style="color:#996600;">&quot;should generate a table from a buttons method&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    last_row<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:columns</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      column <span style="color:#9966CC; font-weight:bold;">do</span>
        <span style="color:#996600;">&quot;foo&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      column <span style="color:#9966CC; font-weight:bold;">do</span>
        <span style="color:#996600;">&quot;bar&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>.<span style="color:#9900CC;">should</span> == <span style="color:#996600;">'&lt;table width=&quot;100%&quot;&gt;&lt;tr&gt;'</span> <span style="color:#006600; font-weight:bold;">+</span>
                    <span style="color:#996600;">'&lt;td class=&quot;left&quot;&gt;foo&lt;/td&gt;'</span> <span style="color:#006600; font-weight:bold;">+</span> 
                    <span style="color:#996600;">'&lt;td class=&quot;right&quot;&gt;bar&lt;/td&gt;'</span> <span style="color:#006600; font-weight:bold;">+</span>
                  <span style="color:#996600;">'&lt;/tr&gt;&lt;/table&gt;'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>After a bunch of fiddling and poking around, I finally made the test (and a couple others) pass with this code in my ApplicationHelper :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">def</span> last_row<span style="color:#006600; font-weight:bold;">&#40;</span>options, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
  markaby <span style="color:#9966CC; font-weight:bold;">do</span>
    table<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:width</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;100%&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      tr <span style="color:#9966CC; font-weight:bold;">do</span>
        LastRowContext.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>, options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:columns</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.
                                  <span style="color:#9900CC;">instance_eval</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<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>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> LastRowContext
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>markaby, columns<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@markaby</span>, <span style="color:#0066ff; font-weight:bold;">@column_count</span>, <span style="color:#0066ff; font-weight:bold;">@column_index</span> = 
                            markaby, columns, <span style="color:#006666;">0</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> column<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    alignment = <span style="color:#9966CC; font-weight:bold;">case</span> <span style="color:#0066ff; font-weight:bold;">@column_index</span> <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">1</span> : <span style="color:#ff3333; font-weight:bold;">:left</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#0066ff; font-weight:bold;">@column_count</span> : <span style="color:#ff3333; font-weight:bold;">:right</span>
    <span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#ff3333; font-weight:bold;">:center</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#0066ff; font-weight:bold;">@markaby</span>.<span style="color:#9900CC;">instance_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      td<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:class</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> alignment, <span style="color:#006600; font-weight:bold;">&amp;</span>block<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>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>I&#8217;m sure this could get cleaned up more; this was the work of less than an hour.  In particular, if you did this often, you could extract a common MarkabyContext superclass that had some convenience methods.  The point is, this is really easy to do, and we shouldn&#8217;t be scared to try &#8220;Language level refactorings&#8221; like this.</p>



 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2007/09/26/customizing-markaby-language-level-refactorings/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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>
		<item>
		<title>Object Mother&#8230;in rails?</title>
		<link>http://onemanswalk.com/work/2007/03/06/object-mother-in-rails/</link>
		<comments>http://onemanswalk.com/work/2007/03/06/object-mother-in-rails/#comments</comments>
		<pubDate>Tue, 06 Mar 2007 16:58:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	That&#8217;s right, the circa 2000 pattern still makes sense today.  Use an &#8220;object mother&#8221; as a test factory to conveniently create objects for your unit tests to bang against.  It will often default values, or have different states in which to create objects.  For example, you might have a new_user, as well [...]]]></description>
			<content:encoded><![CDATA[	<p>That&#8217;s right, the circa 2000 pattern still makes sense today.  Use an &#8220;object mother&#8221; as a test factory to conveniently create objects for your unit tests to bang against.  It will often default values, or have different states in which to create objects.  For example, you might have a new_user, as well as a new_superuser and new_guest method all of which return users.</p>

	<p>Read about the <a href="http://www.martinfowler.com/bliki/ObjectMother.html" title="">original pattern</a></p>

	<p>But why, you ask, not just use rails&#8217; fixtures?  Glad you asked.</p>

	<ol>
		<li>it&#8217;s more intuitive and maintainable to setup the data you need right next to the test</li>
			<li>it&#8217;s easier to create just exactly the objects you need when you have test factory methods</li>
	</ol>

	<p>But don&#8217;t take my word for it.  Let&#8217;s look at some code.</p>

	<p>First off, what does this look like in ruby?  I am creating an &#8220;ObjectMother&#8221; module, which I then just mixin to my tests.  A test might then look like :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#9966CC; font-weight:bold;">include</span> ObjectMother
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> test_delete_project
    project = new_project<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'foo'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    post <span style="color:#ff3333; font-weight:bold;">:delete</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> project.<span style="color:#9900CC;">id</span>
    assert_raise<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">ActiveRecord::RecordNotFound</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> Tag.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>project.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>The magic is in &#8220;new_project&#8221;.  It&#8217;s an entirely pragmatic construct.  If you pass it a string, it will set everything else to acceptable defaults, and use that string as a name.  It looks something like this.</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">module</span> ObjectMother
  <span style="color:#9966CC; font-weight:bold;">def</span> new_project<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
    options = <span style="color:#006600; font-weight:bold;">&#123;</span>:name <span style="color:#006600; font-weight:bold;">=&gt;</span> options<span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#9966CC; font-weight:bold;">if</span> options.<span style="color:#9900CC;">is_a</span>? <span style="color:#CC0066; font-weight:bold;">String</span>
    options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:url_name</span><span style="color:#006600; font-weight:bold;">&#93;</span> = options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:name</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>\W<span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> !options.<span style="color:#9900CC;">has_key</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:url_name</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    Project.<span style="color:#9900CC;">create</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  ...
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>Here in project, it defaults the url_name (which must be unique) from the name you&#8217;ve given it.  However, you could also create a more custom project by running this :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  new_project<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'garage'</span>, <span style="color:#ff3333; font-weight:bold;">:url_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'the_garage'</span>, <span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'foo'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>
</p>



	<p>This is how it works for projects, but it&#8217;s only purpose in life is to make my life easier and reduce the amount of code one has to type or read.  So each type of thing it creates works a little bit differently according to our needs<br />
&#8212;&#8212;<br />
h2. A more complicated example</p>

	<p>I was playing with ferret last week, and wrote a test that looked like this :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#9966CC; font-weight:bold;">def</span> test_across_types
    project = new_project<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'rabbit holes'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    post = new_post<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:subject</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'a rabbit has a big head'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    user = new_user<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:display_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'rabbit head'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#0066ff; font-weight:bold;">@search</span>.<span style="color:#CC0066; font-weight:bold;">string</span> = <span style="color:#996600;">'rabbit'</span>
    assert_find <span style="color:#006600; font-weight:bold;">&#91;</span>project, post, user<span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
    <span style="color:#0066ff; font-weight:bold;">@search</span>.<span style="color:#CC0066; font-weight:bold;">string</span> = <span style="color:#996600;">'rabbit head'</span>
    assert_find <span style="color:#006600; font-weight:bold;">&#91;</span>post, user<span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>My thought process was something along the lines of :</p>

	<ol>
		<li>I want to test that my searcher works across types</li>
			<li>I need to create a project, post, and user that all have a term in them (in different places)</li>
			<li>I want to search for the term, and make sure i get all of them</li>
			<li>I want to search for a term that maybe 2 of them have and make sure I only get those 2</li>
	</ol>

	<p>Writing the test for this part literally took 30 seconds, I didn&#8217;t have to go lookup the fixtures or add a new fixture for my new case.  I also didn&#8217;t have to remember all the things that it takes to make a valid project or post or user.</p>

 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2007/03/06/object-mother-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Helpful Additions To Test::Unit</title>
		<link>http://onemanswalk.com/work/2007/03/06/helpful-additions-to-test-unit/</link>
		<comments>http://onemanswalk.com/work/2007/03/06/helpful-additions-to-test-unit/#comments</comments>
		<pubDate>Tue, 06 Mar 2007 16:54:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	Doing a lot of rails work, I&#8217;m getting a good feel for testing in ruby and rails.  Here are some tricks / snippets I use :

	assert_raises takes a string and/or a class

	I want to be able to write

	
one = Project.new&#40;'one'&#41;
&#160;
projects




	I&#8217;ve done this a few times, but I think cruisecontrol.rb&#8217;s implementation is the most robust [...]]]></description>
			<content:encoded><![CDATA[	<p>Doing a lot of rails work, I&#8217;m getting a good feel for testing in ruby and rails.  Here are some tricks / snippets I use :</p>

	<h2>assert_raises takes a string and/or a class</h2>

	<p>I want to be able to write</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">one = Project.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'one'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
projects</pre></div></div>
</p>



	<p>I&#8217;ve done this a few times, but I think <a href="http://cruisecontrolrb.thoughtworks.com" title="">cruisecontrol.rb&#8217;s</a> implementation is the most robust :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#9966CC; font-weight:bold;">def</span> assert_raises<span style="color:#006600; font-weight:bold;">&#40;</span>arg1 = <span style="color:#0000FF; font-weight:bold;">nil</span>, arg2 = <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    expected_error = arg1.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Exception</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? arg1 : <span style="color:#0000FF; font-weight:bold;">nil</span>
    expected_class = arg1.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#9966CC; font-weight:bold;">Class</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? arg1 : <span style="color:#0000FF; font-weight:bold;">nil</span>
    expected_message = arg1.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">String</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? arg1 : arg2
    <span style="color:#9966CC; font-weight:bold;">begin</span> 
      <span style="color:#9966CC; font-weight:bold;">yield</span>
      <span style="color:#CC0066; font-weight:bold;">fail</span> <span style="color:#996600;">&quot;expected error was not raised&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#6666ff; font-weight:bold;">Test::Unit::AssertionFailedError</span>
      <span style="color:#CC0066; font-weight:bold;">raise</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
      <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#9966CC; font-weight:bold;">if</span> e.<span style="color:#9900CC;">message</span> == <span style="color:#996600;">&quot;expected error was not raised&quot;</span>
      assert_equal<span style="color:#006600; font-weight:bold;">&#40;</span>expected_error, e<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> expected_error
      assert_equal<span style="color:#006600; font-weight:bold;">&#40;</span>expected_class, e.<span style="color:#9966CC; font-weight:bold;">class</span>, <span style="color:#996600;">&quot;Unexpected error type raised&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> expected_class
      assert_equal<span style="color:#006600; font-weight:bold;">&#40;</span>expected_message, e.<span style="color:#9900CC;">message</span>, <span style="color:#996600;">&quot;Unexpected error message&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> expected_message.<span style="color:#9900CC;">is_a</span>? <span style="color:#CC0066; font-weight:bold;">String</span>
      assert_matched<span style="color:#006600; font-weight:bold;">&#40;</span>expected_message, e.<span style="color:#9900CC;">message</span>, <span style="color:#996600;">&quot;Unexpected error message&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> expected_message.<span style="color:#9900CC;">is_a</span>? <span style="color:#CC00FF; font-weight:bold;">Regexp</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>


	<p>&#8212;&#8212;<br />
h2. assert_equal_sets</p>

	<p>In Java, I used to push things into sets and compare them when I didn&#8217;t care about order.  In ruby, I sometimes use assert_equal_sets.  It does a compare of two arrays independent of order.  So</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  assert_equal_sets <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">3</span>, <span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">3</span>, <span style="color:#006666;">5</span>, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>   <span style="color:#008000; font-style:italic;"># passes</span>
  assert_equal_sets <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span>, <span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">3</span>, <span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>    <span style="color:#008000; font-style:italic;"># fails</span></pre></div></div>
</p>



	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Array</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> reorder_like!<span style="color:#006600; font-weight:bold;">&#40;</span>other<span style="color:#006600; font-weight:bold;">&#41;</span>
    tmp = dup
    clear
    other.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#0000FF; font-weight:bold;">self</span></pre></div></div>
</p>


	<p>&#8212;&#8212;<br />
h2. file_sandbox for testing against the file system</p>

	<p>After dragging this code around me for the last 6 or 7 projects I&#8217;ve been on, I finally packaged it up as a <a href="http://rubyforge.org/projects/file_sandbox" title="">gem</a> .  It lets you write code like :</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">in_sandbox <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>sandbox<span style="color:#006600; font-weight:bold;">|</span>
  sandbox.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:file</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'b/a.txt'</span>, <span style="color:#ff3333; font-weight:bold;">:with_contents</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'some stuff'</span>
&nbsp;
  assert_equal <span style="color:#996600;">'some_stuff'</span>, <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span>sandbox.<span style="color:#9900CC;">root</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'/b/a.txt'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>
</p>



	<p>Basically it creates a temporary directory for you to muck about in.  After the block is ended (or teardown is called on your test) that directory and everything in it is guaranteed to be cleaned up.  It also has a bunch of methods to make file based things easier like creating a file, etc.</p>

	<p>Install it with &#8220;gem install file_sandbox&#8221; </p>

 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2007/03/06/helpful-additions-to-test-unit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting Today in a Test</title>
		<link>http://onemanswalk.com/work/2006/12/20/setting-today-in-a-test/</link>
		<comments>http://onemanswalk.com/work/2006/12/20/setting-today-in-a-test/#comments</comments>
		<pubDate>Wed, 20 Dec 2006 19:42:21 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	Ruby is awesome&#8230;

	
  def test_on_weekday
    today_is &#34;May 3, 2006&#34; do
      calendar.add&#40;&#34;a call&#34;&#41;.at&#40;4.pm&#41;.on&#40;:saturday&#41;
&#160;
      assert_equal&#40;Time.local&#40;2006, 5, 6, 16&#41;,
                 calendar.appointments.first.start&#41;
    end
  end





	&#8230;playing with code [...]]]></description>
			<content:encoded><![CDATA[	<p>Ruby is awesome&#8230;</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#9966CC; font-weight:bold;">def</span> test_on_weekday
    today_is <span style="color:#996600;">&quot;May 3, 2006&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      calendar.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;a call&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">at</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">4</span>.<span style="color:#9900CC;">pm</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">on</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:saturday</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
      assert_equal<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;">2006</span>, <span style="color:#006666;">5</span>, <span style="color:#006666;">6</span>, <span style="color:#006666;">16</span><span style="color:#006600; font-weight:bold;">&#41;</span>,
                 calendar.<span style="color:#9900CC;">appointments</span>.<span style="color:#9900CC;">first</span>.<span style="color:#9900CC;">start</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>




	<p>&#8230;playing with code examples for our book.</p>
 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2006/12/20/setting-today-in-a-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Temp Files Pattern</title>
		<link>http://onemanswalk.com/work/2003/12/19/the-temp-files-pattern/</link>
		<comments>http://onemanswalk.com/work/2003/12/19/the-temp-files-pattern/#comments</comments>
		<pubDate>Fri, 19 Dec 2003 00:00:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[file sandbox]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	This is a pattern I first started using while working on rublog w/ Brian Marick et al.  Dave Thomas gave it a better name when he checked it in, and I&#8217;ve renamed it since then.  I think I like temp files.  A second ago, I was working in nfit, and found myself [...]]]></description>
			<content:encoded><![CDATA[	<p>This is a pattern I first started using while working on rublog w/ Brian Marick et al.  Dave Thomas gave it a better name when he checked it in, and I&#8217;ve renamed it since then.  I think I like temp files.  A second ago, I was working in nfit, and found myself writing it in c#.  So I guess I like it.</p>

	<p><strong>Problem:</strong></p>

	<p>You want to test something that accesses files.  Something that you want to do all the time for some apps.</p>

	<p><strong>Solution:</strong></p>

	<p>Create a class called !TempFiles, use it like this (for ruby):</p>

	<p>
<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">def</span> test_load
	files = TempFiles.<span style="color:#9900CC;">new</span>
	files.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'wiki/captions/2003_11-November_captions.txt'</span>, CONTENTS<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
	files.<span style="color:#9900CC;">create</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>dir<span style="color:#006600; font-weight:bold;">|</span>
		captions = Captions.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>Wiki.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>dir, <span style="color:#996600;">'/2003/11-November'</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#996600;">'/2003/11-November'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
		assert_equal<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'Jilian'</span>, captions<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'apPle.JPG'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
		assert_equal<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'from the top'</span>, captions<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'bitter.jpg'</span><span style="color:#006600; font-weight:bold;">&#93;</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>



	<p>It can be used from any test, and makes creating a file easy.  From ruby the blocks make it really nice.  I often add files in the setup code, but always have the block in the actual test.  The nice thing about this is it&#8217;s easy to add files in your test right before creating them.</p>

	<p>I&#8217;ll add the c# usage tomorrow</p>

	<ul>
		<li><span class="caps">UPDATE </span>*</li>
	</ul>

	<p>this is now a gem called file_sandbox &#8211; sudo gem install file_sandbox &#8211; enjoy&#8230;</p>
 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2003/12/19/the-temp-files-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Rid Of Stupid Popup Debug Boxes</title>
		<link>http://onemanswalk.com/work/2003/12/15/getting-rid-of-stupid-popup-debug-boxes/</link>
		<comments>http://onemanswalk.com/work/2003/12/15/getting-rid-of-stupid-popup-debug-boxes/#comments</comments>
		<pubDate>Mon, 15 Dec 2003 00:00:00 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[	Here&#8217;s some wisdom from people.MikeRoberts, that I don&#8217;t want to forget:

	To get rid of those annoying popup dialog boxes prompting you to debug a .net machine, change this registry setting

	HKEY_LOCAL_MACHINESoftwareMicrosoft.NETFramework!DbgJITDebugLaunchSetting

	to &#8216;1&#8217;

	http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconenablingjit-attachdebugging.asp

 ]]></description>
			<content:encoded><![CDATA[	<p>Here&#8217;s some wisdom from people.MikeRoberts, that I don&#8217;t want to forget:</p>

	<p>To get rid of those annoying popup dialog boxes prompting you to debug a .net machine, change this registry setting</p>

	<p><span class="caps">HKEY</span>_LOCAL_MACHINESoftwareMicrosoft.NETFramework!DbgJITDebugLaunchSetting</p>

	<p>to &#8216;1&#8217;</p>

	<p>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconenablingjit-attachdebugging.asp</p>

 ]]></content:encoded>
			<wfw:commentRss>http://onemanswalk.com/work/2003/12/15/getting-rid-of-stupid-popup-debug-boxes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

