<?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>Push cx &#187; testing</title>
	<atom:link href="http://push.cx/tag/testing/feed" rel="self" type="application/rss+xml" />
	<link>http://push.cx</link>
	<description>A traveling geek&#039;s blog on development, games, and the web</description>
	<lastBuildDate>Fri, 14 Oct 2011 10:24:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>From Fixtures to Factories</title>
		<link>http://push.cx/2010/from-fixtures-to-factories</link>
		<comments>http://push.cx/2010/from-fixtures-to-factories#comments</comments>
		<pubDate>Tue, 06 Jul 2010 19:33:02 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[factories]]></category>
		<category><![CDATA[factory_girl]]></category>
		<category><![CDATA[fixtures]]></category>
		<category><![CDATA[Mocha]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://push.cx/?p=1428</guid>
		<description><![CDATA[Automated tests need example data, and it&#8217;s a pain to have to construct a complete object in every test, especially when there are a lot of non-optional fields. The standard improvement is to use fixtures, a file with some example data, that your tests can load by giving the name of a fixture. Here&#8217;s one [...]]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=L3RhZy90ZXN0aW5n">Automated tests</a> need example data, and it&#8217;s a pain to have to construct a complete object in every test, especially when there are a lot of non-optional fields.
</p>

<p>
The standard improvement is to use fixtures, a file with some example data,
that your tests can load by giving the name of a fixture. Here&#8217;s one of the
fixtures for a <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL05lYXJieUdhbWVycy5jb20=">gamer</a>:
</p>

<pre>
alice: # alice is heavily involved
  id: 1
  mail: alice@example.com
  handle: alice
  password: red queen
  teaser: Follow the white rabbit.
  profile: Not so plain, after all.
  homepage: http://www.example.com
  jabber: alice@example.org
  location_id: 3
  last_login: 2006-10-30 11:56:08
  created_at: 2006-10-30 11:56:08
  location_at: 2006-10-30 11:56:08
  tag_text: dnd, wod
  mail_on_message: true
  mail_on_nearby_discussion: true
</pre>

<p>
This is straightforward, but <kbd>location_id</kbd> is the id of another fixture in another file. As you can imagine it&#8217;s pretty easy to get them out-of-sync and break some tests. I end up leaving little <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2MyLmNvbS9jZ2kvd2lraT9Ub05lZWRDb21tZW50cw==">comments</a> in the YAML to remind myself of the cross-reference.
</p>

<p>
It&#8217;s awfully tempting to reuse fixtures from test to test. Maybe Alice was written to test logging in with a username and password and next I&#8217;m writing the test of logging in with an email address and a password. I could copy and paste Alice to another fixture to reuse in that test, but as a programmer I have a pathological aversion to copy and paste. Alice is going to get reused (most of those fields are optional and were used by very different tests), and after this happens a few times it&#8217;s hard to change a fixture without breaking an unrelated test.
</p>

<p>
I&#8217;ve gotten a lot of use out of fixtures as I&#8217;ve developed the habit of <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwOS93aHktaS13cml0ZS10ZXN0cw==">testing all my code</a>, but reuse has made my tests somewhat brittle. After a bit of research, I&#8217;ve moved over to using factories.
</p>

<p>
Here&#8217;s the factory for a gamer:
</p>

<pre>&nbsp;
Factory.<span style="">sequence</span> :handle <span style="color: #66cc66;">&#123;</span> |n| <span style="color: #ff0000;">"Gamer_#{n}"</span> <span style="color: #66cc66;">&#125;</span>
&nbsp;
Factory.<span style="">define</span> :gamer <span style="color: #b1b100;">do</span> |g|
  g.<span style="">handle</span> <span style="color: #66cc66;">&#123;</span> Factory.<span style="">next</span> :handle <span style="color: #66cc66;">&#125;</span>
  g.<span style="">mail</span> <span style="color: #66cc66;">&#123;</span> |m| <span style="color: #ff0000;">"#{m.handle}@example.com"</span> <span style="color: #66cc66;">&#125;</span>
  g.<span style="">password</span> <span style="color: #ff0000;">'secret'</span>
<span style="color: #b1b100;">end</span></pre>

<p>
Only the essential, required fields are listed. Using this simple template, the tests themselves specify the values of the fields they&#8217;ll be testing.
</p>

<pre>&nbsp;
def test_login_with_valid_username_and_password
  gamer = Gamer.<span style="">create</span> :handle =&gt; <span style="color: #ff0000;">'Alice'</span>, :password =&gt; <span style="color: #ff0000;">'red queen'</span>
  ...
<span style="color: #b1b100;">end</span>
def test_login_with_valid_email_and_password
  gamer = Gamer.<span style="">create</span> :mail =&gt; <span style="color: #ff0000;">'alice@example.com'</span>, :password =&gt; <span style="color: #ff0000;">'red queen'</span>
  ...
<span style="color: #b1b100;">end</span></pre>

<p>
Ideally, changing a fixture wouldn&#8217;t break any tests at all. And <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2dpdGh1Yi5jb20vdGhvdWdodGJvdC9mYWN0b3J5X2dpcmw=">factory_girl</a> (what I&#8217;m using for factories in Rails) makes associations easy:
</p>

<pre>&nbsp;
Factory.<span style="">define</span> :post <span style="color: #b1b100;">do</span> |p|
  p.<span style="">association</span> :discussion, :factory =&gt; :discussion
  p.<span style="">association</span> :poster, :factory =&gt; :gamer
  p.<span style="">created_at</span> Time.<span style="">now</span>.<span style="">utc</span>
  p.<span style="">body</span> <span style="color: #ff0000;">"Post Body"</span>
<span style="color: #b1b100;">end</span></pre>

<p>
Now my tests include the specific data they care about, making them easier to understand and improve. And they don&#8217;t interrelate, so they&#8217;re much more reliable. I do still use fixtures, but now they&#8217;re exclusively for tests that need to deal with real-world examples.
</p>
 <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1428" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2010/from-fixtures-to-factories/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Why I Write Tests</title>
		<link>http://push.cx/2009/why-i-write-tests</link>
		<comments>http://push.cx/2009/why-i-write-tests#comments</comments>
		<pubDate>Tue, 13 Jan 2009 14:05:58 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tests]]></category>

		<guid isPermaLink="false">http://push.cx/?p=421</guid>
		<description><![CDATA[I&#8217;ve had a few folks ask me if I really write tests for all my projects, like I mentioned in the last line of my second email in You&#8217;re Not Refactoring. Really? Really, even on personal projects. I write tests because I&#8217;m really good at writing buggy software. I&#8217;d bet I&#8217;m better than you. I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>
I&#8217;ve had a few folks ask me if I really write tests for all my projects, like I mentioned in the last line of my second email in <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwOS95b3VyZS1ub3QtcmVmYWN0b3Jpbmc=">You&#8217;re Not Refactoring</a>. <em>Really?</em>
</p>

<p>
Really, even on personal projects. I write tests because I&#8217;m really good at writing buggy software. I&#8217;d bet I&#8217;m better than you.
</p>

<p>
I&#8217;ve written those bugs that waste hours of time until I found the one missing (or added) bit of punctuation. I&#8217;ve written bugs that first-year programming students have caught. I&#8217;ve written so many of those really obvious bugs that my boss notices with the first glance that you&#8217;d be amazed I&#8217;ve never been fired. Bugs that confused my users into thinking they made mistakes. Bugs that ate data. Bugs that corrupt data silently. And lots of those little changes that broke that totally unrelated thing over there that, wait, yeah, it is actually related in this one tiny way. Bugs that would be really obvious if I&#8217;d remembered that one bug in IE6, if I&#8217;d remembered the API, if I&#8217;d remembered what my own code from last week looked like.
</p>

<p>
I write tests because I&#8217;m imperfect and I hate breaking things. I take code seriously and I&#8217;m happy when I write the best code I can. When I&#8217;m slipping in errors, when I&#8217;m wasting the time of myself and my users, and when I&#8217;m just plain afraid that I might be introducing bugs, I&#8217;m not writing good code. I don&#8217;t keep anything on my nightstand except my glasses, because I know I&#8217;m just going to knock it all off in the middle of the night when I&#8217;m groping for my glasses. I don&#8217;t want to set myself up to fail.
</p>

<p>
Testing <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwNy9hZGVxdWFjeS1pcy1pbmFkZXF1YXRl">isn&#8217;t everything</a>, and I&#8217;ve talked about <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwOC9ub3ctZG8teW91LWtub3ctaXQtd29ya3M=">its limits</a>. But it&#8217;s <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwNy9wYWlubGVzcy11cGdyYWRlLXRvLXJhaWxzLTIw">saved my bacon</a> often enough that I feel like I&#8217;m taking needless risks when I don&#8217;t write tests.
</p>

<p>
Yeah, OK, <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwOS90d2l0dGVyLWFuZC1ydWJ5cy1vcGVuLWNsYXNzZXM=">fun little one-off scripts</a> don&#8217;t have tests, but that script is also about the largest I&#8217;ll let code get without test. Read the comments, someone found that I left a bug lurking to shoot myself in the foot when I tried to expand or reuse the code.
</p>

<p>
I don&#8217;t want to write buggy software, but I know I will, so I write tests and then I build things that are far less buggy. I don&#8217;t test my personal projects out of a sense of obligation, I test them so I can relax and have fun building cool things.
</p>

<p>
That&#8217;s why my advice is always to start small with tests. Test what you can easily, you don&#8217;t have to write exhaustive tests that cover all possibilities on all your code. Write a happy-path test for when a function or method works right, and cover one or two ways things can go wrong or be misused. A few weeks or months from now when you break it in some silly way or don&#8217;t remember how it&#8217;s supposed to be called, the time you invested in tests will pay dividends. When some you narrow down a bug, write a test that exercises it, fix the bug, and smile as your test passes: that bug isn&#8217;t going to slip back into your code again. You&#8217;ll write better code without the stress. Trust me, I know.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=421" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2009/why-i-write-tests/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>You&#8217;re Not Refactoring</title>
		<link>http://push.cx/2009/youre-not-refactoring</link>
		<comments>http://push.cx/2009/youre-not-refactoring#comments</comments>
		<pubDate>Sat, 10 Jan 2009 22:43:16 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Extreme Programming]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://push.cx/?p=400</guid>
		<description><![CDATA[Changing code is a great way to break it, especially in really subtle ways that you won&#8217;t pick up on for weeks or months. The maintainer of Unangband, Andrew Doull, wrote that Refactoring Is Hell. I sent him a note in response to that blog post that I think I may just as well have [...]]]></description>
			<content:encoded><![CDATA[<p>
Changing code is a great way to break it, especially in really subtle ways that you won&#8217;t pick up on for weeks or months. The maintainer of <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3VuYW5nYmFuZC5ibG9nc3BvdC5jb20v">Unangband</a>, Andrew Doull, wrote that <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3JvZ3VlbGlrZWRldmVsb3Blci5ibG9nc3BvdC5jb20vMjAwOC8xMi9yZWZhY3RvcmluZy1oZWxsLmh0bWw=">Refactoring Is Hell</a>. I sent him a note in response to that blog post that I think I may just as well have blogged. So:
</p>

<blockquote>
<p>
I wanted to write to say you&#8217;re not refactoring. The term refactoring
was created in the context of extreme programming [er, I should have said "popularized"] and it was defined as
reworking code that&#8217;s under test. Without the tests, you&#8217;re just
rewriting, working without a net. That&#8217;s why your code ends up buggier.
</p>

<p>
I don&#8217;t write to be pedantic. (Well, OK, maybe a little. I am a
programmer, after all.) I write because I also used to wonder what all
the fuss was about &#8216;refactoring&#8217; when it really just seemed like a
dangerous bunch of nonsense that left my code with new bugs. I didn&#8217;t do
any automated testing at the time and didn&#8217;t realize how valuable it can
be.
</p>

<p>
I still haven&#8217;t drunk the XP kool-aid, but having a good test suite
means I don&#8217;t code in fear that I&#8217;ve broken things, or feel awful when a
bug slips into production because I didn&#8217;t remember to hand-test some
weird situation. I&#8217;ll even write tests on the personal projects I&#8217;m just
hacking away privately at &#8212; yeah, I probably will just do a little unit
testing instead of a really comprehensive, robust system, but even a bit
helps.
</p>
</blockquote>

<p>
He responded that a game like Unangband is especially hard to test because it has so many moving parts and much of the gameplay is generated. It does have a very high-level test, an automated player. I wrote back:
</p>

<blockquote>
<p>
I don&#8217;t really think anything is too complex for test cases. You have a
&#8216;pick up&#8217; command that, if the player is standing on an object and has
inventory space, removes it from the world and puts it in their
inventory. There&#8217;s a fair amount of data structure you have to mock
(which is not the easiest thing in the world in C), but you can know the
basic command works even because you test all the really weird corner
cases about when the player is polymorphed into a creature without hands
or something. I&#8217;d bet as simple as the command usually is, you&#8217;ve broken
&#8216;pick up&#8217; at least a few times. It&#8217;s a safety net you build one strand
at a time, like a spider web. You may still be able to fall through
(especially at first), but sometimes you&#8217;ll catch a strand and be glad
you built it for yourself.
</p>

<p>
The other part of complexity is that thinking about testability changes
how you structure your programs. You force yourself to decouple
different parts of code because you want to be able to test them in
isolation from each other &#8212; that this improves your overall design is a
nice side-effect. That you can spend less time worrying about or kicking
yourself for breaking something &#8216;so obvious!&#8217; is the real one.
</p>

<p>
You may enjoy the book <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5hbWF6b24uY29tL2RwLzAyMDE0ODU2NzIvP3RhZz1wdXNoY3gtMjA=">Refactoring: Improving the Design of Existing
Code</a>, it&#8217;s the classic in the space. Though my money&#8217;s on the later book
<a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5hbWF6b24uY29tL2RwLzAxMzExNzcwNTIvP3RhZz1wdXNoY3gtMjA=">Working Effectively with Legacy Code</a>, which is a couple hundred pages
on how to go from cursing your predecessors to a reliable codebase.
Saved my bacon at a previous job. I know Unangband is your hobby rather
than your job, but I suggest them because they&#8217;ve helped keep my hobby
code more fun than frustrating.
</p>
</blockquote>

<p>
Part of what keeps developers from testing more is <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2FuYW5lbHNvbi5jb20vYmxvZy8yMDA4LzA4L2EtcmFpbHMtYXBwbGljYXRpb24tZmFjdG9yeS8=">friction</a>. You have to pick a test suite, read the docs, learn the API, blow an hour or few chasing a bug because you misunderstood something, figure out the philosophy of it, rewrite your tests so they&#8217;re clear and paradigmatic&#8230;
</p>



<p>
The friction seems to be a lot higher in C/C++ than in dynamic languages. There are good technical reasons for it, but I think at least part of it is cultural. I&#8217;ve noticed a less testing in the Python community than Ruby, and I think that&#8217;s just because Rails was big on testing and has driven so much of the interest in Ruby. The community assumption is that software includes tests.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=400" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2009/youre-not-refactoring/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Now Do You Know It Works?</title>
		<link>http://push.cx/2008/now-do-you-know-it-works</link>
		<comments>http://push.cx/2008/now-do-you-know-it-works#comments</comments>
		<pubDate>Tue, 11 Nov 2008 13:49:47 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[reliability]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[verification]]></category>

		<guid isPermaLink="false">http://push.cx/?p=366</guid>
		<description><![CDATA[You&#8217;re writing code to store a file on Amazon S3. It&#8217;s a popular, powerful, widely-used and highly-reliable service, and you know the Amazon S3 API pretty well. So you write a function that takes a file and a key name (filename), then calls the HTTP PUT to store the data. Do you know it works? [...]]]></description>
			<content:encoded><![CDATA[<p>
You&#8217;re writing code to store a file on Amazon S3. It&#8217;s a popular, powerful, widely-used and highly-reliable service, and you know the Amazon S3 API pretty well. So you write a function that takes a file and a key name (filename), then calls the <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2RvY3MuYW1hem9ud2Vic2VydmljZXMuY29tL0FtYXpvblMzLzIwMDYtMDMtMDEvaW5kZXguaHRtbD9SRVNUT2JqZWN0UFVULmh0bWw=">HTTP PUT</a> to store the data. Do you know it works?
</p>

<p>
Well, you run test it once or twice with a file or two you have laying around and don&#8217;t get an exception and there&#8217;s the file in your S3 file browser. Now do you know it works?
</p>

<p>
There&#8217;s that nagging feeling of doubt that your code might be imperfect that you only get after you&#8217;ve been programming long enough to <em>really</em> screw something up. So you write some unit tests to make sure you&#8217;re calling the API correctly with <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwNy9zaW1wbGUtcnVieS1tb2NraW5n">neat mocking</a> and all that jazz. You&#8217;re an experienced (read: paranoid) developer, so you do more than &#8220;happy path&#8221; testing, you make sure it raises errors when given keys that contain invalid characters or are too long. Now do you know it works?
</p>

<p>
Well, no, you haven&#8217;t seen it in production. So you test it there and you learn that every few thousand HTTP requests, S3 throws a temporary error and it works fine on retry. You change your code to notice this and try again a few times and only then raise an exception. You&#8217;re smart, you encapsulate this somehow so that all your S3 requests use it and you <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2MyLmNvbS9jZ2kvd2lraT9Eb250UmVwZWF0WW91cnNlbGY=">Don&#8217;t Repeat Yourself</a>. Now do you know it works?
</p>

<p>
There&#8217;s no more of those random errors in production, but you don&#8217;t know it works. You know that you PUT a file to S3 without errors, but you don&#8217;t know that you succeeded at the original goal, which was to store a file. The HTTP PUT is the means, not the end.
</p>

<p>
So you turn around and do an <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2RvY3MuYW1hem9ud2Vic2VydmljZXMuY29tL0FtYXpvblMzLzIwMDYtMDMtMDEvUkVTVE9iamVjdEhFQUQuaHRtbA==">HTTP HEAD</a> against that key you just PUT to make sure the <kbd>Content-Length</kbd> matches the size of the file you just uploaded. Heck, if you&#8217;re really paranoid, you turn around and GET the file from S3 and hash it to make sure they both have the same SHA1. You&#8217;re ensuring that S3 serves up the file you thought you stored. (Maybe you even do this from EC2 to avoid the time and charge of downloading the file, but I&#8217;m not going to go into it.) Now do you know it works?
</p>

<p>
No. Here your troubles begin.
</p>

<p>
You have a race condition. If another process does a PUT between your PUT and your HEAD/GET, your function will wrongly think Amazon failed to store the file. You can check that the HEAD&#8217;s <kbd>Last-Modified</kbd> matches the <kbd>Date</kbd> on the PUT so you know the file was replaced since you PUT it there, but at that point all your code can do is PUT it again (and boy will it suck when two copies of this program are running and they both want to have the last word) or your code can throw its hands up and pretend things went fine. Now do you know it works?
</p>

<p>
No, you don&#8217;t. You can&#8217;t know. Computers promise to be deterministic and reliable, but anything with a little complexity turns into a computer <em>system</em> to sneak in side effects and temporal coupling and other beasties and that&#8217;s even before you have to deal with hardware failures or someone turning on the microwave and disrupting your wifi connection&#8230;
</p>

<h2>Finally, You Come to the Point</h2>

<p>
It&#8217;s a trade-off. Even in computers, there is no such thing as perfectly reliable code, there&#8217;s only how much resources you&#8217;re willing to pay for the next improvements. It&#8217;s easy to be oblivious to this. I can&#8217;t even count the number of times  said some variation of &#8220;OK, <em>now</em> I know it works&#8221; before I learned that it will always break. Always.
</p>

<p>
Failure is inevitable. If you want to build anything large and to last, you have to incorporate it into your design and build organic systems that monitor, recover, repair, and, above all, fail gracefully when they themselves inevitably fail.
</p>

<p class="aside">
P.S to folks coding in Ruby: don&#8217;t use <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2FtYXpvbi5ydWJ5Zm9yZ2Uub3JnLw==">AWS::S3</a>, it only checks for valid keys on HTTP PUT and doesn&#8217;t retry transient errors. <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3JpZ2h0YXdzLnJ1Ynlmb3JnZS5vcmcv">RightAWS</a> is an improvement.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=366" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2008/now-do-you-know-it-works/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Painless Upgrade to Rails 2.0</title>
		<link>http://push.cx/2007/painless-upgrade-to-rails-20</link>
		<comments>http://push.cx/2007/painless-upgrade-to-rails-20#comments</comments>
		<pubDate>Thu, 13 Dec 2007 12:10:20 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[ActiveResource]]></category>
		<category><![CDATA[NearbyGamers]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Rails 2.0]]></category>
		<category><![CDATA[routing]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[test]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tests]]></category>
		<category><![CDATA[upgrade]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://push.cx/2007/painless-upgrade-to-rails-20</guid>
		<description><![CDATA[I spent a dead-easy 2.5 hours last night updating NearbyGamers to Rails 2.0. My svn commit message read (with links added here for convenience): Updated to Rails 2.0.1 rm&#8217;d lib/slash_urls.rb: Rails switched from ; to / to separate actions rm&#8217;d lib/resource_requirements.rb: now included in ActiveResources rm&#8217;d app/helpers/tags_helper.rb and gamers_helper.rb that had old controller names and [...]]]></description>
			<content:encoded><![CDATA[<p>
I spent a dead-easy 2.5 hours last night updating <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL05lYXJieUdhbWVycy5jb20=">NearbyGamers</a> to Rails 2.0. My svn commit message read (with links added here for convenience):
</p>

<blockquote>
Updated to Rails 2.0.1

<ul>
<li>rm&#8217;d <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwNy9yYWlscy1zZW1pY29sb25zLW91dC1zbGFzaGVzLWlu">lib/slash_urls.rb</a>: Rails switched from ; to / to separate actions</li>
<li>rm&#8217;d <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2Rldi5ydWJ5b25yYWlscy5vcmcvdGlja2V0Lzc2MzMgd2l0aA0KCmh0dHA6Ly9wdXNoLmN4LzIwMDcvcmFpbHMtc2VtaWNvbG9ucy1vdXQtc2xhc2hlcy1pbgoK">lib/resource_requirements.rb</a>: now included in ActiveResources</li>
<li>rm&#8217;d app/helpers/tags_helper.rb and gamers_helper.rb that had old controller names and were unused anyways</li>
<li>app/controllers/gamers_controller.rb: documented and fixed raw post variable extraction</li><li>updated post_path linkers in many views, controllers, and tests</li>
<li>renamed discussion_anchor_post_path to anchor_discussion_post_path to match the new ordering for nested resource urls</li>
<li>fix tag sorting on <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL05lYXJieUdhbWVycy5jb20vdGFncw==">tag index</a></li>
<li>move tags from /tags/Board+Games to <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL05lYXJieUdhbWVycy5jb20vdGFncy9Cb2FyZF9HYW1lcw==">/tags/Board_Games</a>, as Rails fixed the bug that encoded &#8216; &#8216; as &#8216;+&#8217; in non-get variable parts of the URL</li>
</ul>
</blockquote>

<h2>The Details</h2>

<p>
The first two files I deleted were my patches to URL generation that Rails now includes. The next two helper files I never used, but had had old (singular noun) names.
</p>

<p>
The bit about documentation: the way to get at raw post variables changed, so I tweaked code and left myself a reminder of why I&#8217;m doing it (I had to look at the changelog, which means it wasn&#8217;t self-evident).
</p>

<p>
Nested resources (I have nested <kbd>map.resources :discussions { |d| d.resources :posts }</kbd>) changed from <kbd>discussion_edit_post_url</kbd> to <kbd>edit_discussion_post_url</kbd>, so there was a bunch of places to change that. The next change about anchor is me following this convention with my own convenience method.
</p>

<p>
Tag sorting on the index page was a bug that Snarky pointed out to me. When I made some performance tweaks to that page I forgot to keep sorting tags as they came out of the database. This arguably should&#8217;ve been a revision of its own as it&#8217;s unrelated to the upgrade, but it was an easy one-line bugfix so I let it in.
</p>

<p>
Last, I renamed the tag pages. The tag model&#8217;s <kbd>to_param</kbd> just returns <kbd>self.name</kbd> and Rails would turn &#8220;Board Games&#8221; into &#8220;Board+Games&#8221;. It&#8217;s common but technically incorrect, as + only means space in encoded GET/POST variables. The proper encoding of &#8220;Board%20Games&#8221; was really noisy, so I took a page from Wikipedia&#8217;s pagebook and use underscores instead, like &#8220;Board_Games&#8221;. Was one line in the <kbd>to_param</kbd> and one line in the controller&#8217;s <kbd>load_tag</kbd> before_filter, a dozen lines of tweaking old tests, and seven lines of new tests. Eeeeeasy. 
</p>

<h2>The Verdict</h2>

<p>
Tests, tests, tests. If I didn&#8217;t have a solid test suite, I&#8217;d be noticing bugs two months from now in the least-frequently-exercised bits of code. Not only would I not have remembered to test some of the functionality, I wouldn&#8217;t have remembered the corner cases &#8212; or maybe I&#8217;d just have gotten bored of clicking through pages over and over and ignored the corner cases.
</p>

<p>
None of these were hard fixes and none were frustrating. I&#8217;d probably have finished even sooner if I hadn&#8217;t also been poking <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2ljYW5oYXNjaGVlemJ1cmdlci5jb20vMjAwNy8xMi8xMS9taXRvc2lzLw==">lolcats</a> and chatting with friends. And now I have a big list of shiny new Rails 2.0 features I can put to work in <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL05lYXJieUdhbWVycy5jb20=">NearbyGamers</a>.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=292" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2007/painless-upgrade-to-rails-20/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mocha</title>
		<link>http://push.cx/2007/mocha</link>
		<comments>http://push.cx/2007/mocha#comments</comments>
		<pubDate>Sat, 18 Aug 2007 19:41:31 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Mocha]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tests]]></category>

		<guid isPermaLink="false">http://push.cx/2007/mocha</guid>
		<description><![CDATA[I posted yesterday that I was dropping my own little custom mocking code and considering Mocha. A few hours later I made a pass at it and was blown away. It took very little time to convert my ~600 lines of tests to use Mocha and I spent most of that time just cleaning out [...]]]></description>
			<content:encoded><![CDATA[<p>
I <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwNy9zaW1wbGUtcnVieS1tb2NraW5n">posted yesterday</a> that I was dropping my own little custom mocking code and considering Mocha. A few hours later I made a pass at it and was blown away. It took very little time to convert my ~600 lines of tests to use Mocha and I spent most of that time just cleaning out the cruft that my mocking code had engendered.
</p>

<p>
I&#8217;m now a big fan of Mocha like I am <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwNy9yYWlscy0xMjEtaW1wcmVzc2lvbg==">of Rails</a>. Ruby has a way of allowing coders to write magically polished libraries.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=254" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2007/mocha/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Simple Ruby Mocking</title>
		<link>http://push.cx/2007/simple-ruby-mocking</link>
		<comments>http://push.cx/2007/simple-ruby-mocking#comments</comments>
		<pubDate>Fri, 17 Aug 2007 20:15:29 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[mocking]]></category>
		<category><![CDATA[mocks]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tests]]></category>
		<category><![CDATA[unit tests]]></category>

		<guid isPermaLink="false">http://push.cx/2007/simple-ruby-mocking</guid>
		<description><![CDATA[I mentioned at the end of my last post on testing that I wrote some code to do mocking for my unit tests in Ruby. Writing a small mock library was very much reinventing the wheel, but I needed to do it to earn a deeper understanding of mocks. I&#8217;m writing some code (&#8220;Fetcher&#8221;) to [...]]]></description>
			<content:encoded><![CDATA[<p>
I mentioned at the end of my <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwNy9maXh0dXJlcy1pbi1ydWJ5LXVuaXQtdGVzdHM=">last post on testing</a> that I wrote some code to do mocking for my unit tests in Ruby. Writing a small mock library was very much reinventing the wheel, but I needed to do it to earn a deeper understanding of mocks. 
</p>

<p>
I&#8217;m writing some code (&#8220;Fetcher&#8221;) to talk to a POP3 server, fetch mail, and pass it off to another process. One of the tests deals with what happens when the POP3 server is down or otherwise unreachable.
</p>

<pre>&nbsp;
def test_setup_server_down
  pop3 = Mock.<span style="">new</span>
  pop3.<span style="">expect</span><span style="color: #66cc66;">&#40;</span>:<span style="color: #000000; font-weight: bold;">new</span>, <span style="color: #66cc66;">&#91;</span>MAIL_SERVER, MAIL_POP3_PORT<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> raise Timeout::<span style="color: #006600;">Error</span>.<span style="">new</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"execution expired"</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span>
  f = Fetcher.<span style="">new</span><span style="color: #66cc66;">&#40;</span>pop3<span style="color: #66cc66;">&#41;</span>
  <span style="color: #808080; font-style: italic;"># further assertions that f acts correctly</span>
<span style="color: #b1b100;">end</span></pre>

<p>
This says that the Mock object expects a call to the new method with the given arguments, and when the call happens it runs the block. The block could return anything, but in this case it raises the same error as <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5ydWJ5LWRvYy5vcmcvc3RkbGliL2xpYmRvYy9uZXQvcG9wL3Jkb2MvY2xhc3Nlcy9OZXQvUE9QMy5odG1s">Net::Pop3</a> does when it can&#8217;t contact the server. After that the test can go on to make whatever assertions it needs to verify that the exception was handled properly.
</p>

<p>
The Mock object has a list of <kbd>calls</kbd> it expects to see and keeps a list of how it&#8217;s been <kbd>called</kbd> (yes, this could just be one list with an index but I thought it was mentally simpler this way). The test sets up what calls it should <kbd>expect</kbd> to see with what arguments (or blank for any) and block to run (or blank for none). When a method is called on the mock object, <kbd>method_missing</kbd> logs the call and executes the given block (raising a fuss if the call didn&#8217;t match what it expected). 
</p>

<pre>&nbsp;
class Mock
  attr_reader :calls, :called
&nbsp;
  <span style="color: #808080; font-style: italic;"># the stub arg makes it just record all calls</span>
  def initialize
    <span style="color: #0000ff;">@calls</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #0000ff;">@called</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #b1b100;">end</span>
&nbsp;
  <span style="color: #808080; font-style: italic;"># Pass nil for args to ignore the actual args in the call.</span>
  <span style="color: #808080; font-style: italic;"># Proc is optional; default is empty proc returning nil.</span>
  def expect<span style="color: #66cc66;">&#40;</span>method, *args, &amp;proc<span style="color: #66cc66;">&#41;</span>
    <span style="color: #0000ff;">@calls</span> &lt;&lt; <span style="color: #66cc66;">&#123;</span>:method =&gt; method, :args =&gt; args.<span style="">first</span>, :proc =&gt; <span style="color: #66cc66;">&#40;</span>proc <span style="color: #b1b100;">or</span> Proc.<span style="">new</span><span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span>
  <span style="color: #b1b100;">end</span>
&nbsp;
  def method_missing<span style="color: #66cc66;">&#40;</span>method, *args<span style="color: #66cc66;">&#41;</span>
    <span style="color: #0000ff;">@called</span> &lt;&lt; <span style="color: #66cc66;">&#123;</span>:method =&gt; method, :args =&gt; args<span style="color: #66cc66;">&#125;</span>
&nbsp;
    expect = <span style="color: #0000ff;">@calls</span>.<span style="">shift</span>
    raise <span style="color: #ff0000;">"Unexpected mock call #{method.to_s}(#{args.join(', ')})"</span> <span style="color: #b1b100;">if</span> expect.<span style="">nil</span>?
    raise <span style="color: #ff0000;">"Wrong mock call #{method.to_s}(#{args.join(', ')}); expected #{expect[:method]}(#{expect[:args].join(', ')})"</span> <span style="color: #b1b100;">if</span> method != expect<span style="color: #66cc66;">&#91;</span>:method<span style="color: #66cc66;">&#93;</span> <span style="color: #b1b100;">or</span> <span style="color: #66cc66;">&#40;</span>expect<span style="color: #66cc66;">&#91;</span>:args<span style="color: #66cc66;">&#93;</span> != nil <span style="color: #b1b100;">and</span> args != expect<span style="color: #66cc66;">&#91;</span>:args<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
    expect<span style="color: #66cc66;">&#91;</span>:proc<span style="color: #66cc66;">&#93;</span>.<span style="">call</span><span style="color: #66cc66;">&#40;</span>*args<span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">end</span>
end</pre>

<p>
It&#8217;s a straightforward little object, and I also added some code to raise a fuss if expected calls weren&#8217;t made. This does have the downside that any tests defining their own <kbd>teardown</kbd> need to call <kbd>super</kbd>.
</p>

<pre>&nbsp;
class Mock
  def fail_if_not_empty
    <span style="color: #808080; font-style: italic;"># Empty the call stack so that this obj doesn't throw errors for</span>
    <span style="color: #808080; font-style: italic;"># every later test between now and this object getting gc'd</span>
    calls, <span style="color: #0000ff;">@calls</span> = <span style="color: #0000ff;">@calls</span>, <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
    raise <span style="color: #ff0000;">"Mock calls uncalled: n"</span> + calls.<span style="">collect</span> <span style="color: #66cc66;">&#123;</span> |call| <span style="color: #ff0000;">"#{call[:method]}(#{call[:args]} { #{call[:proc] })"</span> <span style="color: #66cc66;">&#125;</span>.<span style="">join</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">" "</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">unless</span> calls.<span style="">empty</span>?
  <span style="color: #b1b100;">end</span>
end
&nbsp;
class Test::<span style="color: #006600;">Unit</span>::<span style="color: #006600;">TestCase</span>
  def teardown
    finish_mocks
  <span style="color: #b1b100;">end</span>
&nbsp;
  def finish_mocks
    ObjectSpace.<span style="">each_object</span><span style="color: #66cc66;">&#40;</span>Mock<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span> |m|
      <span style="color: #000066;">m</span>.<span style="">fail_if_not_empty</span>
    <span style="color: #b1b100;">end</span>
  <span style="color: #b1b100;">end</span>
end</pre>

<p>
This has been a handy piece of code to test the code I&#8217;ve written in the last two weeks, but it&#8217;s not good enough. I have to use a technique called dependency injection to test <kbd>Fetcher.new</kbd>, where the outside code passes it a POP3 object instead of its <kbd>initialize</kbd> just using Net::POP3. Useful for testing, but my code is badly repetitive when all the instantiation calls have to do this exact same setup. (As an aside, Jacob Proffitt recently started an interesting conversation on <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3NjcnVmZnlsb29raW5nY2F0aGVyZGVyLmNvbS9hcmNoaXZlLzIwMDcvMDgvMDcvZGVwZW5kZW5jeS1pbmplY3Rpb24uYXNweA==">dependency injection</a>, took <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3NjcnVmZnlsb29raW5nY2F0aGVyZGVyLmNvbS9hcmNoaXZlLzIwMDcvMDgvMTUvcmlwcGluZy1vbi1kaS5hc3B4">criticism</a>, and <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3NjcnVmZnlsb29raW5nY2F0aGVyZGVyLmNvbS9hcmNoaXZlLzIwMDcvMDgvMTYvdGlsdGluZy1hdC13aW5kbWlsbHMuYXNweA==">responded</a>. Good reading.)
</p>

<p>
I was pondering how to extend the Mock object to let me mock class methods (eg a call to <kbd>Net::POP3.new</kbd>) when I realized I&#8217;d gone as far as I should down the do-it-yourself road. I&#8217;d heard of <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL21vY2hhLnJ1Ynlmb3JnZS5vcmcv">Mocha</a> and it took me all of ten minutes to think to look at its <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2NoZWF0LmVycnRoZWJsb2cuY29tL3MvbW9jaGEv">cheat sheet</a> where that&#8217;s the first example.
</p>

<p>
After spending two hours or so writing and tweaking this code, the best thing for it is to be thrown away. I&#8217;ve learned about mocking by doing it and I&#8217;m better-prepared to understand someone else&#8217;s larger and better library.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=253" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2007/simple-ruby-mocking/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Fixtures in Ruby Unit Tests</title>
		<link>http://push.cx/2007/fixtures-in-ruby-unit-tests</link>
		<comments>http://push.cx/2007/fixtures-in-ruby-unit-tests#comments</comments>
		<pubDate>Thu, 12 Jul 2007 21:45:47 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[fixtures]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tests]]></category>
		<category><![CDATA[unit tests]]></category>

		<guid isPermaLink="false">http://push.cx/2007/fixtures-in-ruby-unit-tests</guid>
		<description><![CDATA[I&#8217;m writing some Ruby scripts that sort and store lots of small files. After a day or two of hacking I had the basic code working, ran through a few thousand files, and a malformed file blew up the sorter. That was OK, the sorter was intentionally naive and lacking in error handling; I wanted [...]]]></description>
			<content:encoded><![CDATA[<p>
I&#8217;m writing some Ruby scripts that sort and store lots of small files. After a day or two of hacking I had the basic code working, ran through a few thousand files, and a malformed file blew up the sorter. That was OK, the sorter was intentionally naive and lacking in error handling; I wanted it to hack it together and try out a few approaches before committing to serious development.
</p>

<p>
Now it&#8217;s time to treat it as real code, which means getting it under test. Tests are especially useful for ensuring tricky edge cases like malformed input don&#8217;t quietly break during other changes. When I started writing unit tests for the sorter I wanted fixtures similar to the <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL21hbnVhbHMucnVieW9ucmFpbHMuY29tL3JlYWQvY2hhcHRlci8yNg==">Rails fixtures</a> so I could write:
</p>

<pre>&nbsp;
<span style="color: #000066;">require</span> File.<span style="">dirname</span><span style="color: #66cc66;">&#40;</span>__FILE__<span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">'/../test_helper'</span>
<span style="color: #000066;">require</span> <span style="color: #ff0000;">'message'</span>
&nbsp;
class MessageTest &lt; Test::<span style="color: #006600;">Unit</span>::<span style="color: #006600;">TestCase</span>
  fixtures :message
&nbsp;
  def test_initialization
    f = Message.<span style="">new</span> message<span style="color: #66cc66;">&#40;</span>:good<span style="color: #66cc66;">&#41;</span>
    <span style="color: #808080; font-style: italic;"># assert the object was parsed correctly, etc.</span>
  <span style="color: #b1b100;">end</span>
end</pre>

<p>
The basic usage is <kbd>fixtures :type</kbd> to load the data and create the named function (in this case, <kbd>message</kbd>)  for fetching data. First, I wrote some <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3lhbWwub3JnL3N0YXJ0Lmh0bWw=">YAML</a> fixtures for my data and put them in <kbd>test/fixtures/message.yaml</kbd>. You can see the <kbd>:good</kbd> key in use here:
</p>

<pre>
good: |
  [the body of a good message here]
missing_header: |
  [more text]
bad_checksum: |
  [more text]
</pre>

<p>
Because I&#8217;m parsing textfiles my fixtures look pretty simple, but the <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5ydWJ5LWRvYy5vcmcvc3RkbGliL2xpYmRvYy95YW1sL3Jkb2MvaW5kZXguaHRtbA==">YAML library</a> for Ruby makes serialization of any very easy.
</p>

<p>
So here&#8217;s the code I put in <kbd>test/test_helper.rb</kbd> to set this up. I really like the <kbd>[list].flatten.each</kbd> idiom so the call from the unit test class can cleanly pass one or more types, eg: <kbd>fixtures :message</kbd> and <kbd>fixtures :message, :user</kbd>.
</p>

<pre>&nbsp;
class Test::<span style="color: #006600;">Unit</span>::<span style="color: #006600;">TestCase</span>
  <span style="color: #0000ff;">@@fixtures</span> = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
  def self.<span style="">fixtures</span> list
    <span style="color: #66cc66;">&#91;</span>list<span style="color: #66cc66;">&#93;</span>.<span style="">flatten</span>.<span style="">each</span> <span style="color: #b1b100;">do</span> |fixture|
      self.<span style="">class_eval</span> <span style="color: #b1b100;">do</span>
        <span style="color: #808080; font-style: italic;"># add a method name for this fixture type</span>
        define_method<span style="color: #66cc66;">&#40;</span>fixture<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span> |item|
          <span style="color: #808080; font-style: italic;"># load and cache the YAML</span>
          <span style="color: #0000ff;">@@fixtures</span><span style="color: #66cc66;">&#91;</span>fixture<span style="color: #66cc66;">&#93;</span> ||= YAML::<span style="color: #006600;">load_file</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"test/fixtures/#{fixture.to_s}.yaml"</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #0000ff;">@@fixtures</span><span style="color: #66cc66;">&#91;</span>fixture<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>item.<span style="">to_s</span><span style="color: #66cc66;">&#93;</span>
        <span style="color: #b1b100;">end</span>
      <span style="color: #b1b100;">end</span>
    <span style="color: #b1b100;">end</span>
  <span style="color: #b1b100;">end</span>
end</pre>

<p>
(Note to Rails coders: I deliberately left out the pluralization, I like having the File class in <kbd>file.rb</kbd> with the fixtures in <kbd>text/fixtures/file.rb</kbd> and the database table named <kbd>file</kbd> and so on. I don&#8217;t mind Rails&#8217; convention of pluralizing to &#8220;files&#8221; in the latter two examples, but I&#8217;d rather have a single identifier.)
</p>

<p>
Last night at the <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3J1ZG9scGhoZXJpbmdzb2NpZXR5Lm9yZy9pbmRleC5waHA/dGl0bGU9TWFpbl9QYWdl">Rudolph Hering Society</a> meeting <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2Jsb2cuaWFuYmlja2luZy5vcmc=">Ian Bicking</a> and <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3Blb3BsZS5jcy51Y2hpY2Fnby5lZHUvfnZhcm1hYS8=">Atul Varma</a> explained mocking to me. I wrote a (very) little Ruby this morning to do mocking and I&#8217;ll post that in a day or two when I&#8217;m sure I didn&#8217;t do it too badly.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=250" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2007/fixtures-in-ruby-unit-tests/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Adequacy is Inadequate</title>
		<link>http://push.cx/2007/adequacy-is-inadequate</link>
		<comments>http://push.cx/2007/adequacy-is-inadequate#comments</comments>
		<pubDate>Wed, 04 Jul 2007 14:11:37 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[certification]]></category>
		<category><![CDATA[motivation]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://push.cx/2007/adequacy-is-inadequate</guid>
		<description><![CDATA[This post started as a comment on Reg Braitwaithe&#8217;s post Certification? Bring it on! and metastasized into a post of its own. It sounds like you&#8217;ve given up on software development. You&#8217;re drawing a line in the sand at the outside edge of a program to say, &#8220;Here, this far and no further, here is [...]]]></description>
			<content:encoded><![CDATA[<p class="aside">
This post started as a comment on Reg Braitwaithe&#8217;s post <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3dlYmxvZy5yYWdhbndhbGQuY29tLzIwMDcvMDcvY2VydGlmaWNhdGlvbi1icmluZy1pdC1vbi5odG1s">Certification? Bring it on!</a> and metastasized into a post of its own.
</p>

<p>
It sounds like you&#8217;ve given up on software development. You&#8217;re drawing a line in the sand at the outside edge of a program to say, &#8220;Here, this far and no further, here is where I can objectively judge software. This is my one firm place from which I&#8217;ll move the world.&#8221;
</p>

<p>
You&#8217;re not wrong, it&#8217;s a good place to make a stand for quality. But you&#8217;re not right when the nicest thing you can say about a program is, &#8220;Well&#8230; it works.&#8221;
</p>

<p>
I don&#8217;t want to be poisoned when I go to a restaurant, yes, that&#8217;s the minimum bar. But I want fine food, not just fast food. I want to be delighted by my meals, not merely satiated. The software market suffers from a staggering demand and the barely competent who have stumbled up to meet it. If the marketplace could decide quality, the <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy53b3JzZXRoYW5mYWlsdXJlLmNvbQ==">Daily WTF</a> would not exist, but it&#8217;s a <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3dlYmxvZy5yYWdhbndhbGQuY29tLzIwMDcvMDUvbm90LXNvLWJpZy1zb2Z0d2FyZS1hcHBsaWNhdGlvbi5odG1s">market for lemons</a>.
</p>

<p>
I code because I love writing good code. It keeps me up at night &#8212; hell, it <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3B1c2guY3gvMjAwNy9nb29kLXRpbWluZw==">wakes me up at night</a>. I don&#8217;t code so I can tell myself I&#8217;ve adequately performed my duties, I code because I love writing the best code I can and then pushing the definition of what my best is.
</p>

<p>
Maybe I&#8217;m ranting away at a false dichotomy. A certification based on testing is a great idea and I&#8217;d hit the books to earn it. I just don&#8217;t think it&#8217;s <em>enough</em>.
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=249" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2007/adequacy-is-inadequate/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

