<?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>Blog &#124; Rubyyot &#187; Castle</title>
	<atom:link href="http://blog.rubyyot.com/category/programming/castle/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.rubyyot.com</link>
	<description>Programming, Pragmatism and Getting By in the World</description>
	<lastBuildDate>Wed, 24 Mar 2010 00:14:27 +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>Using Factories for testing Castle ActiveRecord: Part 3 : Test Data Builders</title>
		<link>http://blog.rubyyot.com/2009/01/using-factories-for-testing-castle-activerecord-part-3-test-data-builders/</link>
		<comments>http://blog.rubyyot.com/2009/01/using-factories-for-testing-castle-activerecord-part-3-test-data-builders/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 07:30:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Castle]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[test data builder]]></category>

		<guid isPermaLink="false">http://blog.rubyyot.com/?p=123</guid>
		<description><![CDATA[I recently was trying to make a test data factory for Castle ActiveRecord like the Ruby on Rails plugin Machinist and posted about my results.  Today, I read a post titled, Test Data Builders: an alternative to the Object Mother Pattern.
This is a little more work that the previous alternative of writing up a [...]]]></description>
			<content:encoded><![CDATA[<p>I recently was trying to make a test data factory for Castle ActiveRecord like the Ruby on Rails plugin <a href="http://github.com/technoweenie/machinist/tree/master">Machinist </a>and <a href="http://blog.rubyyot.com/2009/01/using-factories-for-testing-castle-activerecord-part-2-rearden/">posted about my results</a>.  Today, I read a post titled, <a href="http://nat.truemesh.com/archives/000714.html">Test Data Builders: an alternative to the Object Mother Pattern.</a><br />
This is a little more work that the previous alternative of writing up a Blueprint file, but I liked the fluent nature of it, and it&#8217;s not all that hard to do.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Castle.ActiveRecord</span><span style="color: #008000;">;</span>
&nbsp;
&nbsp;
<span style="color: #0600FF;">namespace</span> Government.<span style="color: #0000FF;">Models</span>.<span style="color: #0000FF;">Tests</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> PersonBuilder
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> _firstName <span style="color: #008000;">=</span> Faker.<span style="color: #0000FF;">NameFaker</span>.<span style="color: #0000FF;">FirstName</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> _lastName <span style="color: #008000;">=</span> Faker.<span style="color: #0000FF;">NameFaker</span>.<span style="color: #0000FF;">LastName</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> _secretGovernmentId <span style="color: #008000;">=</span> Faker.<span style="color: #0000FF;">NumberFaker</span>.<span style="color: #0000FF;">Number</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">11111111</span>, <span style="color: #FF0000;">9999999999</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> PersonBuilder WithFirstName<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> firstName<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">this</span>._firstName <span style="color: #008000;">=</span> firstName<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> this<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> PersonBuilder WithLastName<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> lastName<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">this</span>._lastName <span style="color: #008000;">=</span> lastName<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> this<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> PersonBuilder WithSecretGovernmentId<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> govID<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">this</span>._secretGovernmentId <span style="color: #008000;">=</span> govID<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> this<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// builds but doesn't save</span>
        <span style="color: #0600FF;">public</span> Person Build<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> Person<span style="color: #000000;">&#40;</span>_secretGovernmentId, _firstName, _lastName<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// builds and then saves and flushes</span>
        <span style="color: #0600FF;">public</span> Person Make<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Person person<span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Person<span style="color: #000000;">&#40;</span>_secretGovernmentId, _firstName, _lastName<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            ActiveRecordMediator<span style="color: #008000;">&lt;</span>person<span style="color: #008000;">&gt;</span>.<span style="color: #0000FF;">SaveAndFlush</span><span style="color: #000000;">&#40;</span>person<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> ActiveRecordMediator<span style="color: #008000;">&lt;</span>person<span style="color: #008000;">&gt;</span>.<span style="color: #0000FF;">FindByPrimaryKey</span><span style="color: #000000;">&#40;</span>person.<span style="color: #0000FF;">Id</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>You can then call it with as many of the With clauses as you like and either save it to the DB or not.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Make and save a random record</span>
Person person <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PersonBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Make</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// Make and save a record with First Name Bill and Secret Government ID of 123456789</span>
Person person <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PersonBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.
                          <span style="color: #0000FF;">WithFirstName</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Bill&quot;</span><span style="color: #000000;">&#41;</span>.
                          <span style="color: #0000FF;">WithSecretGovernmentID</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">123456789</span><span style="color: #000000;">&#41;</span>.
                          <span style="color: #0000FF;">Make</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// Make a random record without saving</span>
Person person <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PersonBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Build</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>That&#8217;s about all there is to it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.rubyyot.com/2009/01/using-factories-for-testing-castle-activerecord-part-3-test-data-builders/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Chores:  A test driven website</title>
		<link>http://blog.rubyyot.com/2009/01/chores-a-test-driven-website/</link>
		<comments>http://blog.rubyyot.com/2009/01/chores-a-test-driven-website/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 05:00:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Castle]]></category>
		<category><![CDATA[Chores]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[webrat]]></category>

		<guid isPermaLink="false">http://blog.rubyyot.com/?p=41</guid>
		<description><![CDATA[I admit it, I don't always write my tests first, and at other times I don't write my tests at all. There I said it. However, I have found that writing tests first has accelerated the speed at which I learn and it has improved the quality of my work.]]></description>
			<content:encoded><![CDATA[<p><em>This is part of <a href="http://blog.rubyyot.com/chores-a-test-driven-website/">the Chores series of posts</a></em></p>
<p>I admit it, I don&#8217;t always write my tests first, and at other times I don&#8217;t write my tests at all. There I said it. However, I have found that writing tests first has accelerated the speed at which I learn and it has improved the quality of my work.</p>
<p>There is a general feeling that people who use TDD see themselves as better than those who don&#8217;t use it.  I don&#8217;t think that this is the case.  I think that this happens because once you start working in these styles, it&#8217;s strange to work without them.  It&#8217;s not that you can&#8217;t or won&#8217;t do it, but it feels like you are working with a reduced toolset when you do.</p>
<p>I&#8217;m no expert at test driven development or similar styles, but the more I use them, the harder it is to imagine not being able to design and shape my objects with automated tests.  I wouldn&#8217;t want to figure out how related objects fit together without the documentation that automated testing provides.</p>
<p>The term test driven fools people into thinking that it&#8217;s all about testing, but it&#8217;s not.  It&#8217;s about creating functional designs with reduced dependencies.  It&#8217;s also about being able to maintain your code.  It&#8217;s about self improvement.</p>
<p>The point of learning to work in these styles is not to be better than those that don&#8217;t.  Instead, the point is to make the code you wrote today better than the code you wrote a week ago, or a month ago. It is also so that when you or someone else looks at the code you wrote today, they will find it to be:</p>
<ul>
<li>working</li>
<li>documented</li>
<li>flexable</li>
<li>extendable</li>
</ul>
<p>There are many articles on the benefits of working in this style, and my point is not to repeat them.  In fact my purpose is to prove them correct, or at least to show that it is not difficult to do and just takes a little discipline.  The same discipline that I lack at times, but I&#8217;m getting better.</p>
<h3>The Project</h3>
<p>I am the father of 4 kids, and they are wonderful children who make messes and generally don&#8217;t clean up after themselves unless told to do so (and sometimes not even then).   My general idea of a website is to built a Rails app that a family can go and if they are parents, keep track of what their kids do or don&#8217;t do.  If they are kids, they can document what they have done and perhaps see much they have earned for the week in allowance.  I have tried doing allowances before, but the problem is that my kids have to be told to do what they are supposed to be doing quite often to make sure they are doing it.   I don&#8217;t want to do this.   I want to provide immediate feedback for them on what they are earning, and I am hoping that this will give them the motivation they need to keep going.  I also want to help them to develop the habits that will continue with or without the draw of rewards.  It&#8217;s a moderately involved project,</p>
<h3>Test or Die</h3>
<p>Because <a title="Test Awareness Month" href="http://www.railstips.org/2009/1/6/test-or-die" target="_blank">January is Test Awareness Month</a> I have decided to document bulding (at least beginning to build) this site on my blog using Cucumber and Test::Unit to drive it.   I am developing currently on a Windows machine, though this may change as the project goes on as my other machines are running Ubuntu.  So without further ado&#8230;</p>
<p><em>Note: (May 11, 2009 &#8211; I originally created this with Rails v2.2.2.  I&#8217;m currently running back through to check compatability with Rails v2.3.2)</em></p>
<h3>Start!</h3>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">$ rails chores
      create
      create  app<span style="color:#006600; font-weight:bold;">/</span>controllers
      create  app<span style="color:#006600; font-weight:bold;">/</span>helpers
      .....
      <span style="color:#9900CC;">create</span>  log<span style="color:#006600; font-weight:bold;">/</span>development.<span style="color:#9900CC;">log</span>
      create  log<span style="color:#006600; font-weight:bold;">/</span>test.<span style="color:#9900CC;">log</span></pre></div></div>

<p>I have my rails app.  The first thing I want to do is get this into source control.  In my case git.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">$ cd chores</pre></div></div>

<p>and</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">$ git init</pre></div></div>

<p>now I don&#8217;t want to store everything in source control.  To to make git ignore files and or paths I create a .gitignore file in the chores directory and it looks like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">tmp<span style="color:#006600; font-weight:bold;">*</span>
log<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>~<span style="color:#008000; font-style:italic;">#]</span></pre></div></div>

<p>what this does is it ignores the tmp and log directories and it also ignores any backup files which contain ~ or #.</p>
<p>Now I can check the status to make sure that the .gitignore file is working.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">$ git status
<span style="color:#008000; font-style:italic;"># On branch master</span>
<span style="color:#008000; font-style:italic;">#</span>
<span style="color:#008000; font-style:italic;"># Initial commit</span>
<span style="color:#008000; font-style:italic;">#</span>
<span style="color:#008000; font-style:italic;"># Untracked files:</span>
<span style="color:#008000; font-style:italic;">#   (use &quot;git add &lt;file&gt;...&quot; to include in what will be committed)</span>
<span style="color:#008000; font-style:italic;">#</span>
<span style="color:#008000; font-style:italic;">#       .gitignore</span>
<span style="color:#008000; font-style:italic;">#       README</span>
<span style="color:#008000; font-style:italic;">#       Rakefile</span>
<span style="color:#008000; font-style:italic;">#       app/</span>
<span style="color:#008000; font-style:italic;">#       config/</span>
<span style="color:#008000; font-style:italic;">#       doc/</span>
<span style="color:#008000; font-style:italic;">#       public/</span>
<span style="color:#008000; font-style:italic;">#       script/</span>
<span style="color:#008000; font-style:italic;">#       test/</span>
nothing added to commit but untracked files present <span style="color:#006600; font-weight:bold;">&#40;</span>use <span style="color:#996600;">&quot;git add&quot;</span> to track<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Followed by</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">$ git add .
&nbsp;
$ git commit <span style="color:#006600; font-weight:bold;">-</span>am <span style="color:#996600;">&quot;initial commit&quot;</span></pre></div></div>

<p>Which adds my site to git&#8217;s index and commits my first commit.  Now I&#8217;m going to backup my git repository to my shared host.  A <a title="Git's your shared host on" href="http://railstips.org/2008/11/24/gitn-your-shared-host-on" target="_blank">good explanation of this can be found here</a>.</p>
<p>Now I already have <a title="Cucumber readme" href="http://wiki.github.com/aslakhellesoy/cucumber/ruby-on-rails" target="_blank">cucumber, webrat and rspec installed as gems.</a>  In order to drive my development with cucumber features I need to set up my app to use cucumber.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">$ ruby script<span style="color:#006600; font-weight:bold;">/</span>generate cucumber
      create  features<span style="color:#006600; font-weight:bold;">/</span>step_definitions
      create  features<span style="color:#006600; font-weight:bold;">/</span>step_definitions<span style="color:#006600; font-weight:bold;">/</span>webrat_steps.<span style="color:#9900CC;">rb</span>
      create  features<span style="color:#006600; font-weight:bold;">/</span>support
      create  features<span style="color:#006600; font-weight:bold;">/</span>support<span style="color:#006600; font-weight:bold;">/</span>env.<span style="color:#9900CC;">rb</span>
      create  features<span style="color:#006600; font-weight:bold;">/</span>support<span style="color:#006600; font-weight:bold;">/</span>paths.<span style="color:#9900CC;">rb</span>
      exists  lib<span style="color:#006600; font-weight:bold;">/</span>tasks
      create  lib<span style="color:#006600; font-weight:bold;">/</span>tasks<span style="color:#006600; font-weight:bold;">/</span>cucumber.<span style="color:#9900CC;">rake</span>
      create  script<span style="color:#006600; font-weight:bold;">/</span>cucumber</pre></div></div>

<p>That does the trick.</p>
<h3>Gotchas</h3>
<p>I&#8217;ve found a couple gotchas at this point when working in Windows.  The first is that <a title="Where did 'a' go?" href="http://blog.rubyyot.com/2009/05/cucumber-lost-the-letter-a-on-windows/" target="_blank"> cucumber doesn&#8217;t display the letter &#8216;a&#8217; in the output</a> if you don&#8217;t modify ./features/support/env.rb with the following line:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#ff6633; font-weight:bold;">$KCODE</span>=<span style="color:#996600;">''</span></pre></td></tr></table></div>

<p>also you will want to make sure you are using <a title="Sqlite3 gem for windows" href="http://blog.rubyyot.com/2009/05/sqlite3-ruby-gem-not-working-on-windows/" target="_blank"> a version of the sqlite gem that supports windows</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">gem <span style="color: #c20cb9; font-weight: bold;">install</span> sqlite3-ruby <span style="color: #660033;">-v</span>=1.2.3</pre></div></div>

<h3>What are your features?</h3>
<p>Cucumber Features are a nice way to be able to document what you want your site to do and verify that it does it.   Probably the easiest way do show this is to do it.  I like to make features that aren&#8217;t too broad or two specific.  I will start with one that is core functionality and save it in the ./features directory as define_chores.feature</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Story:  Define chores
As a parent
I want to define chores
So that I can assign them to my children
&nbsp;
Scenario: Creating a chore
Given I am on the homepage
<span style="color:#9966CC; font-weight:bold;">When</span> I follow “Add Chore”
<span style="color:#9966CC; font-weight:bold;">And</span> I fill <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">&quot;chore[name]&quot;</span> with “My first chore”
<span style="color:#9966CC; font-weight:bold;">And</span> I press <span style="color:#996600;">&quot;Add&quot;</span>
<span style="color:#9966CC; font-weight:bold;">Then</span> I should see “Chore added.”
<span style="color:#9966CC; font-weight:bold;">And</span> I should see “My first chore”</pre></div></div>

<p>Now we have written our first feature. It is a pretty simple one, but as you can see it describes our intent in readable English. Now let&#8217;s fire up cucumber and see what we get.</p>
<p><em><strong>Update:</strong> The following issue was fixed when I tried it in Rails 2.3.2 and cucumber 0.3.1.</em></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">$ cucumber features
c:<span style="color:#006600; font-weight:bold;">/</span>Ruby<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>ruby<span style="color:#006600; font-weight:bold;">/</span>site_ruby<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1.8</span><span style="color:#006600; font-weight:bold;">/</span>rubygems<span style="color:#006600; font-weight:bold;">/</span>custom_require.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">31</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`gem_original_require': no such file to load -- webrat/rspec-rails (MissingSourceFile)</span></pre></div></div>

<p>Hmmm..  what happened here.   Cucumber uses the ./features/support/env.rb file for configuration let&#8217;s look there.  Sure enough there is a require statement for &#8216;webrat/rspec-rails&#8217;.  <a title="Google" href="http://www.google.com/search?sourceid=navclient&#038;ie=UTF-8&#038;rlz=1T4GGLL_enUS308US308&#038;q=webrat%2frspec-rails+cucumber+error" target="_blank">Let&#8217;s google it. </a>  Looks like the offending require is to add some webrat matchers to rspec.   I&#8217;m just going to comment it out for now and try again.</p>
<p><em><strong>Update:</strong> Using the features rake task (rake features from the command line) at this point in Rails 2.3.2 and cucumber 0.3.1 caused the following error:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">rake aborted!
no such file to <span style="color:#CC0066; font-weight:bold;">load</span> <span style="color:#006600; font-weight:bold;">--</span> <span style="color:#006600; font-weight:bold;">/</span>home<span style="color:#006600; font-weight:bold;">/</span>rubyyot<span style="color:#006600; font-weight:bold;">/</span>working<span style="color:#006600; font-weight:bold;">/</span>chores<span style="color:#006600; font-weight:bold;">/</span>db<span style="color:#006600; font-weight:bold;">/</span>schema.<span style="color:#9900CC;">rb</span></pre></div></div>

<p>This is because we have not yet defined any database tables.  To fix this we simply have to create an empty file in the correct location.  In Windows you can navigate to the folder and create a new file.  In Linux you can type from your rails root:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">touch db<span style="color:#006600; font-weight:bold;">/</span>schema.<span style="color:#9900CC;">rb</span></pre></div></div>

<p></em></p>
<p>This time I get:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">$ rake features
Story:  Define chores
As a parent
I want to define chores
So that I can assign them to my children
&nbsp;
  Scenario: Creating a chore                          <span style="color:#008000; font-style:italic;"># features/define_chores.feature:6</span>
    Given I am on the homepage                        <span style="color:#008000; font-style:italic;"># features/step_definitions/webrat_steps.rb:6</span>
      No route matches <span style="color:#996600;">&quot;/&quot;</span> with <span style="color:#006600; font-weight:bold;">&#123;</span>:method<span style="color:#006600; font-weight:bold;">=&gt;</span>:get<span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">ActionController::RoutingError</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">eval</span><span style="color:#006600; font-weight:bold;">&#41;</span>:<span style="color:#006666;">2</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">'/^I am on (.+)$/'</span>
      features<span style="color:#006600; font-weight:bold;">/</span>define_chores.<span style="color:#9900CC;">feature</span>:<span style="color:#006666;">7</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">'Given I am on the homepage'</span>
    <span style="color:#9966CC; font-weight:bold;">When</span> I follow “Add Chore”                         <span style="color:#008000; font-style:italic;"># features/define_chores.feature:8</span>
    <span style="color:#9966CC; font-weight:bold;">And</span> I fill <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">&quot;chore[name]&quot;</span> with “My first chore” <span style="color:#008000; font-style:italic;"># features/define_chores.feature:9</span>
    <span style="color:#9966CC; font-weight:bold;">And</span> I press <span style="color:#996600;">&quot;Add&quot;</span>                                 <span style="color:#008000; font-style:italic;"># features/step_definitions/webrat_steps.rb:14</span>
    <span style="color:#9966CC; font-weight:bold;">Then</span> I should see “Chore added.”                  <span style="color:#008000; font-style:italic;"># features/define_chores.feature:11</span>
    <span style="color:#9966CC; font-weight:bold;">And</span> I should see “My first chore”                 <span style="color:#008000; font-style:italic;"># features/define_chores.feature:12</span>
&nbsp;
<span style="color:#006666;">1</span> scenario
<span style="color:#006666;">1</span> failed step
<span style="color:#006666;">1</span> skipped step
<span style="color:#006666;">4</span> undefined steps
&nbsp;
You can implement step definitions <span style="color:#9966CC; font-weight:bold;">for</span> undefined steps with these snippets:
&nbsp;
<span style="color:#9966CC; font-weight:bold;">When</span> <span style="color:#006600; font-weight:bold;">/</span>^I follow “Add Chore”$<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  pending
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">When</span> <span style="color:#006600; font-weight:bold;">/</span>^I fill <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">&quot;([^<span style="color:#000099;">\&quot;</span>]*)&quot;</span> with “My first chore”$<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>arg1<span style="color:#006600; font-weight:bold;">|</span>
  pending
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">Then</span> <span style="color:#006600; font-weight:bold;">/</span>^I should see “Chore added\.”$<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  pending
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">Then</span> <span style="color:#006600; font-weight:bold;">/</span>^I should see “My first chore”$<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  pending
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
rake aborted!
Command failed with status <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>: <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">/</span>usr<span style="color:#006600; font-weight:bold;">/</span>bin<span style="color:#006600; font-weight:bold;">/</span>ruby1.8 <span style="color:#006600; font-weight:bold;">-</span>I <span style="color:#996600;">&quot;/usr/lib/ruby/gems/1....]
&nbsp;
(See full trace by running task with --trace)</span></pre></div></div>

<h2>So what happened here?</h2>
<p>Cucumber loads up all the features and all of the steps and then uses regular expressions to match the features to the steps.  Then it executes the code associated with each step in order.  Notice that it knew what &#8220;Given I am on the homepage&#8221; meant.  That is because <a title="Built-in webrat steps" target = '_blank' href="http://blog.rubyyot.com/2009/04/listing-of-cucumbers-out-of-the-box-webrat-steps/">Webrat has already pre-loaded some common rails testing steps for us.</a></p>
<p>So what error are we having here?  It says &#8220;No route matches &#8220;/&#8221; with {:method=>:get}&#8221;.</p>
<h2>What are routes?</h2>
<p>Routes in Rails have some pretty nifty tricks up their sleeves.  Routes map an incoming request to an action on a controller.  They live in config/routes.rb where you will find some good auto-generated documentation on how to configure them.  In this case we need to create a route for our home page.  This is accomplished by adding</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  map.<span style="color:#9900CC;">root</span> <span style="color:#ff3333; font-weight:bold;">:controller</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'home'</span></pre></div></div>

<p>somewhere after</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">ActionController::Routing::Routes</span>.<span style="color:#9900CC;">draw</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>map<span style="color:#006600; font-weight:bold;">|</span></pre></div></div>

<p>and before</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This tells rails that any requests that come in for the base url should be routed to the HomeController.  Next time we can start working on building this object.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.rubyyot.com/2009/01/chores-a-test-driven-website/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using Factories for testing Castle ActiveRecord: Part 2 : Rearden</title>
		<link>http://blog.rubyyot.com/2009/01/using-factories-for-testing-castle-activerecord-part-2-rearden/</link>
		<comments>http://blog.rubyyot.com/2009/01/using-factories-for-testing-castle-activerecord-part-2-rearden/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 07:26:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Castle]]></category>
		<category><![CDATA[Rearden]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://blog.rubyyot.com/?p=31</guid>
		<description><![CDATA[earlier I posted about using factories for testing Castle ActiveRecord.  I now have an update that I call Rearden.  Here is is]]></description>
			<content:encoded><![CDATA[<p>Earlier <a title="Testing Castle ActiveRecord with Factories" href="http://blog.rubyyot.com/?p=17" target="_blank">I posted about using factories for testing Castle ActiveRecord</a>. I now have an update that I call Rearden.  Here is is:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Castle.ActiveRecord</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> Rearden
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">delegate</span> <span style="color: #0600FF;">void</span> ModifyClass<span style="color: #000000;">&#40;</span>ActiveRecordClass instance, IDictionary overrides<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">///</span>
    <span style="color: #008080; font-style: italic;">/// This is a factory to create test objects for Castle ActiveRecord.</span>
    <span style="color: #008080; font-style: italic;">///</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> Factory
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// overload to call without overrides</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> ActiveRecordClass Init<span style="color: #000000;">&#40;</span>ModifyClass modifyClass<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> Init<span style="color: #000000;">&#40;</span>modifyClass, <span style="color: #008000;">new</span> Dictionary<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// instanciate and set values in ActiveRecordClass</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> ActiveRecordClass Init<span style="color: #000000;">&#40;</span>ModifyClass modifyClass, IDictionary overrides<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            ActiveRecordClass instance <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>ActiveRecordClass<span style="color: #000000;">&#41;</span>Activator.<span style="color: #0000FF;">CreateInstance</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>ActiveRecordClass<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            modifyClass<span style="color: #000000;">&#40;</span>instance, overrides<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> instance<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// overload to call without overrides</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> ActiveRecordClass Make<span style="color: #000000;">&#40;</span>ModifyClass modifyClass<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> Make<span style="color: #000000;">&#40;</span>modifyClass, <span style="color: #008000;">new</span> Dictionary<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// create, save and flush ActiveRecordClass</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> ActiveRecordClass Make<span style="color: #000000;">&#40;</span>ModifyClass modifyClass, IDictionary overrides<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            ActiveRecordClass instance <span style="color: #008000;">=</span> Init<span style="color: #000000;">&#40;</span>modifyClass, overrides<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            ActiveRecordBase asBase <span style="color: #008000;">=</span> instance <span style="color: #0600FF;">as</span> ActiveRecordBase<span style="color: #008000;">;</span>
            asBase.<span style="color: #0000FF;">SaveAndFlush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> instance<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Add the compiled Rearden Assembly to your Test Assembly and then create a Blueprint class:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> Quality.<span style="color: #0000FF;">Model</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> Blueprints
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Blueprint<span style="color: #000000;">&#40;</span>Employee emp, IDictionary overrides<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            emp.<span style="color: #0000FF;">FirstName</span> <span style="color: #008000;">=</span> overrides.<span style="color: #0000FF;">ContainsKey</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;FirstName&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>overrides<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;FirstName&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">:</span> Faker.<span style="color: #0000FF;">NameFaker</span>.<span style="color: #0000FF;">FirstName</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            emp.<span style="color: #0000FF;">LastName</span> <span style="color: #008000;">=</span> overrides.<span style="color: #0000FF;">ContainsKey</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;LastName&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>overrides<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;LastName&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">:</span> Faker.<span style="color: #0000FF;">NameFaker</span>.<span style="color: #0000FF;">LastName</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            emp.<span style="color: #0000FF;">Xid</span> <span style="color: #008000;">=</span> overrides.<span style="color: #0000FF;">ContainsKey</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Xid&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>overrides<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Xid&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">:</span> Faker.<span style="color: #0000FF;">StringFaker</span>.<span style="color: #0000FF;">Randomize</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;X######&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Blueprint<span style="color: #000000;">&#40;</span>Foo foo, IDictionary overrides<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            foo.<span style="color: #0000FF;">Month</span> <span style="color: #008000;">=</span> overrides.<span style="color: #0000FF;">ContainsKey</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Month&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #000000;">&#40;</span>DateTime<span style="color: #000000;">&#41;</span>overrides<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Month&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">:</span> <span style="color: #008000;">new</span> DateTime<span style="color: #000000;">&#40;</span>Faker.<span style="color: #0000FF;">NumberFaker</span>.<span style="color: #0000FF;">Number</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2000</span>, <span style="color: #FF0000;">2009</span><span style="color: #000000;">&#41;</span>, Faker.<span style="color: #0000FF;">NumberFaker</span>.<span style="color: #0000FF;">Number</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">12</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            foo.<span style="color: #0000FF;">Employee</span> <span style="color: #008000;">=</span> overrides.<span style="color: #0000FF;">ContainsKey</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Employee&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #000000;">&#40;</span>Employee<span style="color: #000000;">&#41;</span>overrides<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Employee&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">:</span>  <span style="color: #000000;">&#40;</span>Employee<span style="color: #000000;">&#41;</span>Rearden.<span style="color: #0000FF;">Factory</span>.<span style="color: #0000FF;">Make</span><span style="color: #000000;">&#40;</span>Blueprint<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And now your test looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Can_find_by_Xid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
&nbsp;
            _values<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Xid&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;X123456&quot;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Dictionary</span>
            Rearden.<span style="color: #0000FF;">Factory</span>.<span style="color: #0000FF;">Make</span><span style="color: #000000;">&#40;</span>Blueprints.<span style="color: #0000FF;">Blueprint</span>, _values<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Employee fromDB <span style="color: #008000;">=</span> Employee.<span style="color: #0000FF;">FindByXid</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;X123456&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;X123456&quot;</span>, fromDB.<span style="color: #0000FF;">Xid</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.rubyyot.com/2009/01/using-factories-for-testing-castle-activerecord-part-2-rearden/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Factories for testing Castle ActiveRecord</title>
		<link>http://blog.rubyyot.com/2009/01/using-factories-for-testing-castle-activerecord/</link>
		<comments>http://blog.rubyyot.com/2009/01/using-factories-for-testing-castle-activerecord/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 00:20:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Castle]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://blog.rubyyot.com/?p=17</guid>
		<description><![CDATA[Yesterday, John Nunemaker declared January to be Test Awareness Month in his post Test or Die.  The post is aimed at the Rails community but why stop there?]]></description>
			<content:encoded><![CDATA[<p>Yesterday, John Nunemaker declared January to be Test Awareness Month in his post <a title="Test or Die" href="http://railstips.org/2009/1/6/test-or-die" target="_blank">Test or Die</a>.  The post is aimed at the Rails community but why stop there?  I&#8217;ve been working with Castle ActiveRecord today and since I&#8217;ve liked the plugin Machinist for Rails I decided to use the idea of using a factory to create test data.   The basic idea is that the factory class will create your record in the database that you would like to test.  There is <a title="Machinist for testing Rails" href="http://advent2008.hackruby.com/past/2008/12/5/machinist_for_your_test_data_factory_/" target="_blank">a nice post on how to use it with Faker on the Advent2008 blog</a>. </p>
<p>I found a version of <a title="Faker" href="http://www.codeplex.com/faker" target="_blank">Faker for .Net </a>and wired up a test factory for an ActiveRecord Employee class as follows.   I&#8217;m sure there is plenty of room for future refactoring and abasraction, but here is the basic idea:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> EmployeeFactory
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// user set values</span>
        <span style="color: #0600FF;">private</span> IDictionary _values <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> IDictionary Values
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span><span style="color: #0600FF;">return</span> _values<span style="color: #008000;">;</span><span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> _values <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// clears user set values</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> ResetValues<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            _values.<span style="color: #0000FF;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// creates an instance with data but does not save it</span>
        <span style="color: #0600FF;">public</span> Employee Init<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Employee emp <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Employee<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            emp.<span style="color: #0000FF;">FirstName</span> <span style="color: #008000;">=</span> _values.<span style="color: #0000FF;">ContainsKey</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;FirstName&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> _values<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;FirstName&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">:</span> Faker.<span style="color: #0000FF;">NameFaker</span>.<span style="color: #0000FF;">FirstName</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            emp.<span style="color: #0000FF;">LastName</span> <span style="color: #008000;">=</span> _values.<span style="color: #0000FF;">ContainsKey</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;LastName&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> _values<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;LastName&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">:</span> Faker.<span style="color: #0000FF;">NameFaker</span>.<span style="color: #0000FF;">LastName</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            emp.<span style="color: #0000FF;">Xid</span> <span style="color: #008000;">=</span> _values.<span style="color: #0000FF;">ContainsKey</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Xid&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> emp.<span style="color: #0000FF;">Xid</span> <span style="color: #008000;">=</span> _values<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Xid&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">:</span> Faker.<span style="color: #0000FF;">StringFaker</span>.<span style="color: #0000FF;">Randomize</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;X######&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> emp<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">//creates, saves and flushes</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Make<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Init<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">SaveAndFlush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// creates x number of Employee records.</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Make<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> times<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> times<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                Make<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>This allowed me to write tests such as:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">   <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> EmployeeTestCase <span style="color: #008000;">:</span> AbstractModelTestCase
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">private</span> EmployeeFactory _factory<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>SetUp<span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> SetUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            _factory <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> EmployeeFactory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            PrepareScope<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Can_find_by_Xid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            _factory.<span style="color: #0000FF;">Values</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Xid&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;X123456&quot;</span><span style="color: #008000;">;</span>
            _factory.<span style="color: #0000FF;">Make</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Employee fromDB <span style="color: #008000;">=</span> Employee.<span style="color: #0000FF;">FindByXid</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;X123456&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;X123456&quot;</span>, fromDB.<span style="color: #0000FF;">Xid</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Xid_should_be_unique<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            _factory.<span style="color: #0000FF;">Values</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Xid&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;X123456&quot;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">try</span>
            <span style="color: #000000;">&#123;</span>
                _factory.<span style="color: #0000FF;">Make</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Assert.<span style="color: #0000FF;">Fail</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Xid is not set to be unique&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Castle.<span style="color: #0000FF;">ActiveRecord</span>.<span style="color: #0000FF;">Framework</span>.<span style="color: #0000FF;">ActiveRecordException</span> ex<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                Assert.<span style="color: #0000FF;">That</span><span style="color: #000000;">&#40;</span>ExceptionMessageContains<span style="color: #000000;">&#40;</span>ex, <span style="color: #666666;">&quot;Violation of UNIQUE KEY constraint&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Search_Should_Search_Xid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            _factory.<span style="color: #0000FF;">Values</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Xid&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;X123456&quot;</span><span style="color: #008000;">;</span>
            _factory.<span style="color: #0000FF;">Make</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            _factory.<span style="color: #0000FF;">ResetValues</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            _factory.<span style="color: #0000FF;">Make</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            Employee<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> emps <span style="color: #008000;">=</span> Employee.<span style="color: #0000FF;">Search</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;X123456&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, emps.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;X123456&quot;</span>, emps<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Xid</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Search_Should_Search_LastName<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            _factory.<span style="color: #0000FF;">Values</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;LastName&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;MyLast&quot;</span><span style="color: #008000;">;</span>
            _factory.<span style="color: #0000FF;">Make</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            _factory.<span style="color: #0000FF;">ResetValues</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            _factory.<span style="color: #0000FF;">Make</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            Employee<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> emps <span style="color: #008000;">=</span> Employee.<span style="color: #0000FF;">Search</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;MyLast&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, emps.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;MyLast&quot;</span>, emps<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">LastName</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>TearDown<span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> TearDown<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            DisposeScope<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>I am looking to improve on this code as I use it, but I&#8217;m rather happy with how well it&#8217;s working so far.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.rubyyot.com/2009/01/using-factories-for-testing-castle-activerecord/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
