<?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; Chores</title>
	<atom:link href="http://blog.rubyyot.com/tag/chores-rails-programming/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>Chores: a test driven website &#8211; Part 9 (the lost episode)</title>
		<link>http://blog.rubyyot.com/2009/05/chores-a-test-driven-website-part-9-the-lost-episode/</link>
		<comments>http://blog.rubyyot.com/2009/05/chores-a-test-driven-website-part-9-the-lost-episode/#comments</comments>
		<pubDate>Sun, 10 May 2009 13:00:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Chores]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://blog.rubyyot.com/?p=290</guid>
		<description><![CDATA[This is part of the Chores series of posts.
Well it&#8217;s been a while since National Testing month and there has been a lot going on both in my world and in the Ruby Community.  I&#8217;ve been doing a good deal of reading, deployed a Monorail website and been generally busy.  Unfortunately, I haven&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>This is part of the <a href="http://blog.rubyyot.com/chores-a-test-driven-website/ ">Chores series of posts</a>.</p>
<p>Well it&#8217;s been a while since National Testing month and there has been a lot going on both in my world and in the Ruby Community.  I&#8217;ve been doing a good deal of reading, deployed a Monorail website and been generally busy.  Unfortunately, I haven&#8217;t focused on building Chores, my test driven chore delivering website.</p>
<p>Today I&#8217;ve changed all that by dusting off chores and updating my gems.  The first thing that I noticed was that I needed to add some configuration for Webrat for it to function within Cucumber.  I needed to add the following to the env.rb file:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Webrat.<span style="color:#9900CC;">configure</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>config<span style="color:#006600; font-weight:bold;">|</span>
  config.<span style="color:#9900CC;">mode</span> = <span style="color:#ff3333; font-weight:bold;">:rails</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Next I see that I&#8217;m getting an error on the new action for Children because the @child variable is nil.  Let&#8217;s write a functional test to fix that.</p>
<p>Here is my first pass at the functional test and controller</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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'test_helper'</span>
<span style="color:#9966CC; font-weight:bold;">class</span> ChildrenCanCrud <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::TestCase</span>
  tests ChildrenController
&nbsp;
  specify <span style="color:#996600;">&quot;test that it should respond to index&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    get <span style="color:#ff3333; font-weight:bold;">:index</span>
    assert_response <span style="color:#ff3333; font-weight:bold;">:success</span>
    assert_not_nil assigns<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:children</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that it should pass child on new&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    get <span style="color:#ff3333; font-weight:bold;">:new</span>
    assert_response <span style="color:#ff3333; font-weight:bold;">:success</span>
    assert_not_nil assigns<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:child</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that it should create child&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    assert_difference <span style="color:#996600;">'Child.count'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      post <span style="color:#ff3333; font-weight:bold;">:create</span>, <span style="color:#ff3333; font-weight:bold;">:child</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:parent</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Identity.<span style="color:#9900CC;">make</span>, <span style="color:#ff3333; font-weight:bold;">:identity</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Identity.<span style="color:#9900CC;">make</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    assert_redirected_to children_url
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>./app/models/child.rb</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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Child <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:open_identifier</span>
  before_create <span style="color:#ff3333; font-weight:bold;">:set_identity</span>
&nbsp;
  belongs_to <span style="color:#ff3333; font-weight:bold;">:identity</span>,             <span style="color:#ff3333; font-weight:bold;">:foreign_key</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:child_id</span>,
                                    <span style="color:#ff3333; font-weight:bold;">:class_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Identity'</span>
&nbsp;
  belongs_to <span style="color:#ff3333; font-weight:bold;">:parent</span>,               <span style="color:#ff3333; font-weight:bold;">:foreign_key</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:parent_id</span>,
                                    <span style="color:#ff3333; font-weight:bold;">:class_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Identity'</span>
&nbsp;
  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:open_identifier</span>,  <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'can not be blank.'</span>,
                                           <span style="color:#ff3333; font-weight:bold;">:on</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:create</span>
  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:identity</span>,         <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'can not be blank.'</span>,
                                           <span style="color:#ff3333; font-weight:bold;">:on</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:update</span>
  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:parent</span>,    <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'can not be blank.'</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> set_identity
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">identity</span> = Identity.<span style="color:#9900CC;">find_or_make_if_valid</span> <span style="color:#0066ff; font-weight:bold;">@open_identifier</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>./app/models/identity.rb</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
70
71
72
73
74
75
76
77
78
79
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Identity <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> OpenIdAuthentication
&nbsp;
  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:identifier</span>,        <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;can not be blank.&quot;</span>
&nbsp;
  validates_uniqueness_of <span style="color:#ff3333; font-weight:bold;">:identifier</span>,      <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;is already in use, please use another.&quot;</span>
&nbsp;
  validates_length_of <span style="color:#ff3333; font-weight:bold;">:identifier</span>,          <span style="color:#ff3333; font-weight:bold;">:maximum</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">100</span>,
                                            <span style="color:#ff3333; font-weight:bold;">:allow_nil</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>,
                                            <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;is too long.&quot;</span>
&nbsp;
  validate_on_create <span style="color:#ff3333; font-weight:bold;">:valid_id</span>
&nbsp;
  has_many <span style="color:#ff3333; font-weight:bold;">:parents</span>,             <span style="color:#ff3333; font-weight:bold;">:foreign_key</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:parent_id</span>,
                                 <span style="color:#ff3333; font-weight:bold;">:class_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Child'</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:children</span>,            <span style="color:#ff3333; font-weight:bold;">:foreign_key</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:child_id</span>,
                                 <span style="color:#ff3333; font-weight:bold;">:class_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Child'</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> open_id
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">identifier</span>.<span style="color:#0000FF; font-weight:bold;">nil</span>?
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">identifier</span><span style="color:#006600; font-weight:bold;">&#91;</span>7..<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> identifier= identifier
    <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#996600;">&quot;Cannot update identifier.&quot;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> new_record?
    set_identifier identifier
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> open_id= open_id
    <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#996600;">&quot;Cannot update open_id.&quot;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> new_record?
    set_identifier open_id
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> valid_id
    errors.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:identifier</span>, <span style="color:#996600;">&quot;is not a valid Open ID.&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@invalid_id</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">find_by_open_id</span> open_id
    id = OpenIdAuthentication::normalize_identifier<span style="color:#006600; font-weight:bold;">&#40;</span>open_id<span style="color:#006600; font-weight:bold;">&#41;</span>
    find_by_identifier open_id
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">find_or_make_if_valid</span> open_id
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      id = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">find_by_open_id</span> Identity.<span style="color:#9900CC;">normalize_identifier</span><span style="color:#006600; font-weight:bold;">&#40;</span>open_id<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#6666ff; font-weight:bold;">OpenIdAuthentication::InvalidOpenId</span>
      <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">unless</span> id
      id = Identity.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:identifier</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> open_id
      id.<span style="color:#9900CC;">save</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    id
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> Identity.<span style="color:#9900CC;">normalize_identifier</span> id
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      identifier = OpenIdAuthentication::normalize_identifier<span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#6666ff; font-weight:bold;">OpenIdAuthentication::InvalidOpenId</span>
      identifier = <span style="color:#0000FF; font-weight:bold;">nil</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    identifier
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  protected
  <span style="color:#9966CC; font-weight:bold;">def</span> normalize_id id
    identifier = Identity.<span style="color:#9900CC;">normalize_identifier</span> id
    <span style="color:#9966CC; font-weight:bold;">unless</span> identifier
      <span style="color:#0066ff; font-weight:bold;">@invalid_id</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
      identifier = id
    <span style="color:#9966CC; font-weight:bold;">end</span>
    identifier
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> set_identifier id
    write_attribute<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:identifier</span>, normalize_id<span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>and</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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ChildrenController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  <span style="color:#9966CC; font-weight:bold;">def</span> index
    <span style="color:#0066ff; font-weight:bold;">@children</span> = Child.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> new
    <span style="color:#0066ff; font-weight:bold;">@child</span> = Child.<span style="color:#9900CC;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> create
    <span style="color:#0066ff; font-weight:bold;">@child</span> = Child.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:child</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@child</span>.<span style="color:#9900CC;">save</span>
      flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:message</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Child added.&quot;</span>
      redirect_to children_url
    <span style="color:#9966CC; font-weight:bold;">else</span>
      flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:error</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Error saving Child.&quot;</span>
      render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span>  <span style="color:#996600;">&quot;new&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>update to ./test/unit/child_test.rb</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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'test_helper'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> ChildShouldBeValidated <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::TestCase</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that a parent is required&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    child = Child.<span style="color:#9900CC;">new</span>
    child.<span style="color:#9900CC;">open_identifier</span> = valid_open_id
    invalid child
    child.<span style="color:#9900CC;">parent</span> = Identity.<span style="color:#9900CC;">make</span>
    valid child
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that a child is required&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    child = Child.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:parent</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Identity.<span style="color:#9900CC;">make</span>
    invalid child
    child.<span style="color:#9900CC;">open_identifier</span> = valid_open_id
    valid child
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;message for a null identity&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    child = Child.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:parent</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Identity.<span style="color:#9900CC;">make</span>
    validation_message_for child, <span style="color:#ff3333; font-weight:bold;">:open_identifier</span>, <span style="color:#996600;">&quot;can not be blank.&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;message for a null parent&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    child = Child.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:identity</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Identity.<span style="color:#9900CC;">make</span>
    validation_message_for child, <span style="color:#ff3333; font-weight:bold;">:parent</span>, <span style="color:#996600;">&quot;can not be blank.&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> ChildShouldCreateIdentity <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::TestCase</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that open_identifier is used to pass the OpenId&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    child = Child.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:parent</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Identity.<span style="color:#9900CC;">make</span>
    child.<span style="color:#9900CC;">open_identifier</span> = valid_open_id
    assert child.<span style="color:#9900CC;">save</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>./test/functional/children_controller_test.rb</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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'test_helper'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> ChildrenCanCrud <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::TestCase</span>
  tests ChildrenController
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> setup
    <span style="color:#0066ff; font-weight:bold;">@identity</span> = Identity.<span style="color:#9900CC;">make</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;test that it should respond to index&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    get <span style="color:#ff3333; font-weight:bold;">:index</span>, <span style="color:#0000FF; font-weight:bold;">nil</span>, build_session_hash_for<span style="color:#006600; font-weight:bold;">&#40;</span>@identity<span style="color:#006600; font-weight:bold;">&#41;</span>
    assert_response <span style="color:#ff3333; font-weight:bold;">:success</span>
    assert_not_nil assigns<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:children</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that it should pass child on new&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    get <span style="color:#ff3333; font-weight:bold;">:new</span>, <span style="color:#0000FF; font-weight:bold;">nil</span>, build_session_hash_for<span style="color:#006600; font-weight:bold;">&#40;</span>@identity<span style="color:#006600; font-weight:bold;">&#41;</span>
    assert_response <span style="color:#ff3333; font-weight:bold;">:success</span>
    assert_not_nil assigns<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:child</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that it should create child&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    assert_difference <span style="color:#996600;">'Child.count'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      post <span style="color:#ff3333; font-weight:bold;">:create</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:child <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:open_identifier</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;cheese.example.com&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, build_session_hash_for<span style="color:#006600; font-weight:bold;">&#40;</span>@identity<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    assert_redirected_to children_url
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>./test/test_helper.rb</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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">ENV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;RAILS_ENV&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;test&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;/../config/environment&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;/blueprints&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'test_help'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'machinist'</span> <span style="color:#008000; font-style:italic;">#if you installed as gem rather than plugin</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'faker'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::TestCase</span>
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">use_transactional_fixtures</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">use_instantiated_fixtures</span>  = <span style="color:#0000FF; font-weight:bold;">false</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> invalid model
    assert !model.<span style="color:#9900CC;">valid</span>?
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> valid model
    assert model.<span style="color:#9900CC;">valid</span>?
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> validation_message_for model, column, message
    invalid model
    assert_equal message, model.<span style="color:#9900CC;">errors</span>.<span style="color:#9900CC;">on</span><span style="color:#006600; font-weight:bold;">&#40;</span>column<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> valid_identifier
    <span style="color:#996600;">&quot;http://test.example.com/&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> build_session_hash_for identity
    <span style="color:#0066ff; font-weight:bold;">@session_hash</span> = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">'identity_id'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> identity.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> valid_open_id
    <span style="color:#996600;">&quot;test.example.com&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> invalid_open_id
    <span style="color:#996600;">&quot;bad_id&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> specify <span style="color:#006600; font-weight:bold;">*</span>args, <span style="color:#006600; font-weight:bold;">&amp;</span>block
  test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>.. interestingly enough I find that my view that I wrote at some point expects a field called nickname</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="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span> form_for<span style="color:#006600; font-weight:bold;">&#40;</span>@child<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">error_messages</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:nickname</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:nickname</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:open_identifier</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:open_identifier</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">submit</span> <span style="color:#996600;">&quot;Add&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></td></tr></table></div>

<p>So I quickly create and run a migration to add the field.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">~<span style="color: #000000; font-weight: bold;">/</span>working<span style="color: #000000; font-weight: bold;">/</span>chores <span style="color: #7a0874; font-weight: bold;">&#40;</span>chores9<span style="color: #7a0874; font-weight: bold;">&#41;</span>$ ruby script<span style="color: #000000; font-weight: bold;">/</span>generate migration add_nickname_to_child nickname:string
      exists  db<span style="color: #000000; font-weight: bold;">/</span>migrate
      create  db<span style="color: #000000; font-weight: bold;">/</span>migrate<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">20090402191006</span>_add_nickname_to_child.rb</pre></div></div>

<p>which can should be migrated with.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">rake db:migrate</pre></div></div>

<p> <a title="Chores on Github" href="http://github.com/rubyyot/chores/tree/master" target="_blank">Here is the design I settled on.</a></p>
<p>As you can see, I&#8217;ve pushed it out to github.  I&#8217;ve tagged it with &#8220;post_9&#8243;.  I am hoping this will help those that want to follow along and reduce the amount of code that I need to post while writing this up.  Maybe I&#8217;ll write up a post on git and github.  <ins datetime="2009-05-10T05:12:45+00:00"></ins></p>
<p><em><strong>Update</strong>: I apologize this is all getting muddled in my brain as well. Between a change in rails versions and the passage of time. I hope to get this all straightened out <a href="http://blog.rubyyot.com/2009/06/chores-a-test-driven-website-part-10/" target="_blank">in the next post</a>. </em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.rubyyot.com/2009/05/chores-a-test-driven-website-part-9-the-lost-episode/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Chores: a test driven website &#8211; Part 8 (this time it&#039;s personal)</title>
		<link>http://blog.rubyyot.com/2009/01/chores-a-test-driven-website-part-8-this-time-its-personal/</link>
		<comments>http://blog.rubyyot.com/2009/01/chores-a-test-driven-website-part-8-this-time-its-personal/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 19:55:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Chores]]></category>

		<guid isPermaLink="false">http://blog.rubyyot.com/?p=231</guid>
		<description><![CDATA[This is part of the Chores series of posts.
I&#8217;m now attacking the Idenitity model.  My idea for this is a model that contains a single field.  That field is a valid, properly formatted, immutable OpenID.  Because I&#8217;m going to need to validate OpenIDs I&#8217;m going to install the Open ID Plugin as [...]]]></description>
			<content:encoded><![CDATA[<p>This is part of the <a href="http://blog.rubyyot.com/chores-a-test-driven-website/ ">Chores series of posts</a>.</p>
<p>I&#8217;m now attacking the Idenitity model.  My idea for this is a model that contains a single field.  That field is a valid, properly formatted, immutable OpenID.  Because I&#8217;m going to need to validate OpenIDs I&#8217;m going to install the <a href="http://agilewebdevelopment.com/plugins/openidauthentication">Open ID Plugin</a> as follows.</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>plugin install git:<span style="color:#006600; font-weight:bold;">//</span>github.<span style="color:#9900CC;">com</span><span style="color:#006600; font-weight:bold;">/</span>rails<span style="color:#006600; font-weight:bold;">/</span>open_id_authentication.<span style="color:#9900CC;">git</span>
Initialized empty Git repository <span style="color:#9966CC; font-weight:bold;">in</span> c:<span style="color:#006600; font-weight:bold;">/</span>rails<span style="color:#006600; font-weight:bold;">/</span>chores<span style="color:#006600; font-weight:bold;">/</span>vendor<span style="color:#006600; font-weight:bold;">/</span>plugins<span style="color:#006600; font-weight:bold;">/</span>open_id_authentication<span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#9900CC;">git</span><span style="color:#006600; font-weight:bold;">/</span>
remote: Counting objects: <span style="color:#006666;">35</span>, done.←<span style="color:#006600; font-weight:bold;">&#91;</span>K
remote: Compressing objects: <span style="color:#006666;">100</span><span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">31</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">31</span><span style="color:#006600; font-weight:bold;">&#41;</span>, done.←<span style="color:#006600; font-weight:bold;">&#91;</span>K
remote: Total <span style="color:#006666;">35</span> <span style="color:#006600; font-weight:bold;">&#40;</span>delta <span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span>, reused <span style="color:#006666;">21</span> <span style="color:#006600; font-weight:bold;">&#40;</span>delta <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>←<span style="color:#006600; font-weight:bold;">&#91;</span>K
Unpacking objects: <span style="color:#006666;">100</span><span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">35</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">35</span><span style="color:#006600; font-weight:bold;">&#41;</span>, done.
<span style="color:#9900CC;">From</span> git:<span style="color:#006600; font-weight:bold;">//</span>github.<span style="color:#9900CC;">com</span><span style="color:#006600; font-weight:bold;">/</span>rails<span style="color:#006600; font-weight:bold;">/</span>open_id_authentication
 <span style="color:#006600; font-weight:bold;">*</span> branch            HEAD       <span style="color:#006600; font-weight:bold;">-&gt;</span> FETCH_HEAD</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">$ rake open_id_authentication:db:create
<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#9966CC; font-weight:bold;">in</span> c:<span style="color:#006600; font-weight:bold;">/</span>rails<span style="color:#006600; font-weight:bold;">/</span>chores<span style="color:#006600; font-weight:bold;">&#41;</span>
      exists  db<span style="color:#006600; font-weight:bold;">/</span>migrate
      create  db<span style="color:#006600; font-weight:bold;">/</span>migrate<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">20090125184610</span>_add_open_id_authentication_tables.<span style="color:#9900CC;">rb</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">$ rake db:migrate
<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#9966CC; font-weight:bold;">in</span> c:<span style="color:#006600; font-weight:bold;">/</span>rails<span style="color:#006600; font-weight:bold;">/</span>chores<span style="color:#006600; font-weight:bold;">&#41;</span>
==  AddOpenIdAuthenticationTables: migrating ==================================
<span style="color:#006600; font-weight:bold;">--</span> create_table<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:open_id_authentication_associations</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:force<span style="color:#006600; font-weight:bold;">=&gt;</span>true<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#006600; font-weight:bold;">-&gt;</span> 0.0310s
<span style="color:#006600; font-weight:bold;">--</span> create_table<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:open_id_authentication_nonces</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:force<span style="color:#006600; font-weight:bold;">=&gt;</span>true<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#006600; font-weight:bold;">-&gt;</span> 0.0160s
==  AddOpenIdAuthenticationTables: migrated <span style="color:#006600; font-weight:bold;">&#40;</span>0.0470s<span style="color:#006600; font-weight:bold;">&#41;</span> =========================</pre></div></div>

<p>Here is my test for the Identity model</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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'test_helper'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> IdentityShouldBeValidatedTest <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::TestCase</span>
  specify <span style="color:#996600;">&quot;that an open ID is required on create&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    identity = Identity.<span style="color:#9900CC;">new</span>
    invalid identity
    identity.<span style="color:#9900CC;">open_id</span> = <span style="color:#996600;">&quot;test.example.com&quot;</span>
    valid identity
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;message for an overly long identifier&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    identity = Identity.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:identifier</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> hundred_and_one_character_identifier
    validation_message_for identity, <span style="color:#ff3333; font-weight:bold;">:identifier</span>, <span style="color:#996600;">&quot;is too long.&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;message for a null identifier&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    identity = Identity.<span style="color:#9900CC;">new</span>
    validation_message_for identity, <span style="color:#ff3333; font-weight:bold;">:identifier</span>, <span style="color:#996600;">&quot;can not be blank.&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;message for a poorly formatted&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    identity = Identity.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:identifier</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> invalid_open_id
    validation_message_for identity, <span style="color:#ff3333; font-weight:bold;">:identifier</span>, <span style="color:#996600;">&quot;is not a valid Open ID.&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;message for a duplicate identifier&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    Identity.<span style="color:#9900CC;">make</span> <span style="color:#ff3333; font-weight:bold;">:identifier</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> valid_identifier
    identity = Identity.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:identifier</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> valid_identifier
    validation_message_for identity, <span style="color:#ff3333; font-weight:bold;">:identifier</span>, <span style="color:#996600;">&quot;is already in use, please use another.&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that 100 character identifier is valid&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    identity = Identity.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:identifier</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> hundred_character_identifier
    valid identity
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that identifier is unique&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    Identity.<span style="color:#9900CC;">make</span> <span style="color:#ff3333; font-weight:bold;">:identifier</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> valid_identifier
    identity = Identity.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:identifier</span> <span style="color:#006600; font-weight:bold;">=&gt;</span>  valid_identifier
    invalid identity
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that open_id must have the correct format&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    identity = Identity.<span style="color:#9900CC;">new</span>
    identity.<span style="color:#9900CC;">open_id</span> = invalid_open_id
    invalid identity
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that open_id uniqueness extends to identifier when validated&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    Identity.<span style="color:#9900CC;">make</span>  <span style="color:#ff3333; font-weight:bold;">:identifier</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> valid_identifier
    identity = Identity.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:open_id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;test.example.com&quot;</span>
    invalid identity
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  protected
    <span style="color:#9966CC; font-weight:bold;">def</span> hundred_character_identifier
      <span style="color:#996600;">&quot;http://This.is.a.hundred.character.description.one.hundred.chatacters.is.not.too.long.but.it.of.com/&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> hundred_and_one_character_identifier
      <span style="color:#996600;">&quot;http://This.is.a.hundred1.character.description.one.hundred.chatacters.is.not.too.long.but.it.of.com/&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> invalid_open_id
      <span style="color:#996600;">&quot;bad_id&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> IdentityOpenIdShouldSetIdentifierTest <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::TestCase</span>
  specify <span style="color:#996600;">&quot;that open_id sets identifier&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    identity = Identity.<span style="color:#9900CC;">new</span>
    assert_nil identity.<span style="color:#9900CC;">identifier</span>
    identity.<span style="color:#9900CC;">open_id</span> = <span style="color:#996600;">&quot;test.example.com&quot;</span>
    assert_not_nil identity.<span style="color:#9900CC;">identifier</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that identifier sets open_id&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    identity = Identity.<span style="color:#9900CC;">new</span>
    assert_nil identity.<span style="color:#9900CC;">open_id</span>
    identity.<span style="color:#9900CC;">identifier</span> = <span style="color:#996600;">&quot;http://test.example.com/&quot;</span>
    assert_not_nil identity.<span style="color:#9900CC;">open_id</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that open_id is nicely formatted for display&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    identity = Identity.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:identifier</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;http://test.example.com/&quot;</span>
    assert_equal valid_open_id, identity.<span style="color:#9900CC;">open_id</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> IdentityShouldBeImmutableTest <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::TestCase</span>
  specify <span style="color:#996600;">&quot;that open_id is immutable&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    identity = Identity.<span style="color:#9900CC;">make</span>
    assert_raise <span style="color:#CC00FF; font-weight:bold;">RuntimeError</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      identity.<span style="color:#9900CC;">open_id</span> = <span style="color:#996600;">&quot;monkey.example.com&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that identifier is immutable&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    identity = Identity.<span style="color:#9900CC;">make</span>
    assert_raise <span style="color:#CC00FF; font-weight:bold;">RuntimeError</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      identity.<span style="color:#9900CC;">identifier</span> = <span style="color:#996600;">&quot;http://monkey.example.com/&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> IdentityShouldBeFoundByOpenID <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::TestCase</span>
  specify <span style="color:#996600;">&quot;that open_id can be used to find a record&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    identity = Identity.<span style="color:#9900CC;">make</span> <span style="color:#ff3333; font-weight:bold;">:identifier</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> valid_identifier
    identity_from_db = Identity.<span style="color:#9900CC;">find_by_open_id</span> valid_open_id
    assert_equal identity.<span style="color:#9900CC;">identifier</span>, identity.<span style="color:#9900CC;">identifier</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that display formatted open_id can be used to find a record&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    identity = Identity.<span style="color:#9900CC;">make</span> <span style="color:#ff3333; font-weight:bold;">:identifier</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> valid_identifier
    identity_from_db = Identity.<span style="color:#9900CC;">find_by_open_id</span> valid_identifier
    assert_equal identity.<span style="color:#9900CC;">identifier</span>, identity.<span style="color:#9900CC;">identifier</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Here is the code that passes the test<br />
<em>./test/test_helper.rb</em></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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">ENV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;RAILS_ENV&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;test&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;/../config/environment&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;/blueprints&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'test_help'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'faker'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::TestCase</span>
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">use_transactional_fixtures</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">use_instantiated_fixtures</span>  = <span style="color:#0000FF; font-weight:bold;">false</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> invalid model
    assert !model.<span style="color:#9900CC;">valid</span>?
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> valid model
    assert model.<span style="color:#9900CC;">valid</span>?
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> validation_message_for model, column, message
    invalid model
    assert_equal message, model.<span style="color:#9900CC;">errors</span>.<span style="color:#9900CC;">on</span><span style="color:#006600; font-weight:bold;">&#40;</span>column<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> valid_identifier
    <span style="color:#996600;">&quot;http://test.example.com/&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> valid_open_id
    <span style="color:#996600;">&quot;test.example.com&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> specify <span style="color:#006600; font-weight:bold;">*</span>args, <span style="color:#006600; font-weight:bold;">&amp;</span>block
  test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p><em>./app/models/identity.rb</em></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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Identity <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> OpenIdAuthentication
&nbsp;
  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:identifier</span>,        <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;can not be blank.&quot;</span>
&nbsp;
  validates_uniqueness_of <span style="color:#ff3333; font-weight:bold;">:identifier</span>,      <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;is already in use, please use another.&quot;</span>
&nbsp;
  validates_length_of <span style="color:#ff3333; font-weight:bold;">:identifier</span>,          <span style="color:#ff3333; font-weight:bold;">:maximum</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">100</span>,
                                            <span style="color:#ff3333; font-weight:bold;">:allow_nil</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>,
                                            <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;is too long.&quot;</span>
&nbsp;
  validate_on_create <span style="color:#ff3333; font-weight:bold;">:valid_id</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> open_id
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">identifier</span>.<span style="color:#0000FF; font-weight:bold;">nil</span>?
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">identifier</span><span style="color:#006600; font-weight:bold;">&#91;</span>7..<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> identifier= identifier
    <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#996600;">&quot;Cannot update identifier.&quot;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> new_record?
    set_identifier identifier
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> open_id= open_id
    <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#996600;">&quot;Cannot update open_id.&quot;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> new_record?
    set_identifier open_id
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> valid_id
    errors.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:identifier</span>, <span style="color:#996600;">&quot;is not a valid Open ID.&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@invalid_id</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">find_by_open_id</span> open_id
    id = OpenIdAuthentication::normalize_identifier<span style="color:#006600; font-weight:bold;">&#40;</span>open_id<span style="color:#006600; font-weight:bold;">&#41;</span>
    find_by_identifier open_id
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  protected
  <span style="color:#9966CC; font-weight:bold;">def</span> normalize_id id
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      identifier = normalize_identifier<span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#6666ff; font-weight:bold;">OpenIdAuthentication::InvalidOpenId</span>
      identifier = id
      <span style="color:#0066ff; font-weight:bold;">@invalid_id</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    identifier
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> set_identifier id
    write_attribute<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:identifier</span>, normalize_id<span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p><em>./test/blueprints.rb</em></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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'machinist'</span>  <span style="color:#008000; font-style:italic;"># if you installed machinist from gem</span>
&nbsp;
Chore.<span style="color:#9900CC;">blueprint</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  description <span style="color:#996600;">&quot;Put the dirty clothes in the hamper&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
Identity.<span style="color:#9900CC;">blueprint</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  identifier <span style="color:#996600;">&quot;http://#{Faker::Name.first_name}.#{Faker::Internet.domain_name}/&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
Child.<span style="color:#9900CC;">blueprint</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  child <span style="color:#006600; font-weight:bold;">&#123;</span> Identity.<span style="color:#9900CC;">make</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  parent <span style="color:#006600; font-weight:bold;">&#123;</span> Identity.<span style="color:#9900CC;">make</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>This one took a little work to do and I&#8217;m going to revisit it in a little while to see if it needs any refactoring.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.rubyyot.com/2009/01/chores-a-test-driven-website-part-8-this-time-its-personal/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Chores: a test driven website &#8211; part 5 (the admission)</title>
		<link>http://blog.rubyyot.com/2009/01/chores-a-test-driven-website-part-5/</link>
		<comments>http://blog.rubyyot.com/2009/01/chores-a-test-driven-website-part-5/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 00:00:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Chores]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[functional testing]]></category>

		<guid isPermaLink="false">http://blog.rubyyot.com/?p=147</guid>
		<description><![CDATA[This is part of the Chores series of posts
While committing Chores to git after the last refactoring, I&#8217;m thinking to myself.  Who am I kidding?  I&#8217;m going to write a post on functional testing?  But I suck at functional testing!  Well, we&#8217;ll see how it goes.
First, I&#8217;d like to install a [...]]]></description>
			<content:encoded><![CDATA[<p><em>This is part of <a href="http://blog.rubyyot.com/chores-a-test-driven-website/" target="_blank">the Chores series of posts</a></em></p>
<p>While committing Chores to git after the last refactoring, I&#8217;m thinking to myself.  Who am I kidding?  I&#8217;m going to write a post on functional testing?  But I suck at functional testing!  Well, we&#8217;ll see how it goes.</p>
<p>First, I&#8217;d like to install a gem called <a href="http://github.com/notahat/machinist/tree/master" target="_blank">Machinist</a> (this is also available as a plugin, if you prefer).  Machinist is a drop in replacement for fixtures, allowing you to make (save) validated test data with a little config and a method call.  You can read all about it in the readme, linked above.  To install the gem simply:</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> notahat-machinist</pre></div></div>

<p>And be sure to configure your gem by adding the following to config/environments/test.rb</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">&quot;notahat-machinist&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:lib</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'machinist'</span>, <span style="color:#ff3333; font-weight:bold;">:version</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'0.3.1'</span></pre></div></div>

<p>In fact, something I failed to cover is <a href="http://blog.rubyyot.com/2009/05/configuring-gems-for-your-rails-app/" title='Configuring gems in Rails' target='_blank'>configuring your gems</a>.  Something you might want to take a minute to do.</p>
<h2>Blueprints</h2>
<p>Machinist uses &#8220;blueprints&#8221; to create records in the database for testing. These blueprints tell machinist how to create a valid record / object.  These are stored in a blueprints file.  So lets create a file in  test/ called blueprints.rb that contains:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">Chore.<span style="color:#9900CC;">blueprint</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  description <span style="color:#996600;">&quot;Put the dirty clothes in the hamper&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>and require the blueprint file in our test helper (test/test_helper.rb)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'machinist'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;/blueprints&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>On to the functional tests, or testing of the controllers.  I used the rails scaffold functional tests as the starting point for my functional tests for chore.  Here&#8217;s how the first run ended up.  You might notice that there is not test for update, that&#8217;s because I&#8217;m not sure update is needed at the moment and since our cucmber feature (remember we are trying to get that working) didn&#8217;t say anything about updating, I&#8217;m just leaving it off for now.</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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'test_helper'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> ChoresCanCrud <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::TestCase</span>
  tests ChoresController
&nbsp;
  specify <span style="color:#996600;">&quot;test that it should respond to index&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    get <span style="color:#ff3333; font-weight:bold;">:index</span>
    assert_response <span style="color:#ff3333; font-weight:bold;">:success</span>
    assert_not_nil assigns<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:chores</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that it should pass chore on new&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    get <span style="color:#ff3333; font-weight:bold;">:new</span>
    assert_response <span style="color:#ff3333; font-weight:bold;">:success</span>
    assert_not_nil assigns<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:chore</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that it should create chore&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    assert_difference <span style="color:#996600;">'Chore.count'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      post <span style="color:#ff3333; font-weight:bold;">:create</span>, <span style="color:#ff3333; font-weight:bold;">:chore</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'My Chore'</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    assert_redirected_to chores_url
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that it will destroy a chore, muahahahahaha&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    chore = Chore.<span style="color:#9900CC;">make</span>
    assert_difference <span style="color:#996600;">'Chore.count'</span>, <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      delete <span style="color:#ff3333; font-weight:bold;">:destroy</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> chore.<span style="color:#9900CC;">id</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    assert_redirected_to chores_url
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>The corresponding Chores controller looks like this</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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ChoresController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  <span style="color:#9966CC; font-weight:bold;">def</span> index
    <span style="color:#0066ff; font-weight:bold;">@chores</span> = Chore.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> new
    <span style="color:#0066ff; font-weight:bold;">@chore</span> = Chore.<span style="color:#9900CC;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> create
    <span style="color:#0066ff; font-weight:bold;">@chore</span> = Chore.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:chore</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@chore</span>.<span style="color:#9900CC;">save</span>
    redirect_to chores_url
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> destroy
    chore = Chore.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    chore.<span style="color:#9900CC;">destroy</span>
    redirect_to chores_url
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Additionally, I had to make a couple views to get the tests to run.  They are skeletal at this point, barely more than an empty file.  In fact ./app/views/chores/index.html.erb is an empty file and ./app/views/chores/new.html.erb is</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span> form_for<span style="color:#006600; font-weight:bold;">&#40;</span>@chore<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">error_messages</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
&nbsp;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">submit</span> <span style="color:#996600;">&quot;Add&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></td></tr></table></div>

<p>Looking back at the functional tests, this seems a little on the light side.  For instance the tests passed without ever checking to see what happened with save of Chore.  I&#8217;m going to go back and add a couple mote tests. Adding the following two tests</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="ruby" style="font-family:monospace;">  specify <span style="color:#996600;">&quot;that it should send a message that chore was created&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    post <span style="color:#ff3333; font-weight:bold;">:create</span>, <span style="color:#ff3333; font-weight:bold;">:chore</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'My Chore'</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    assert_equal <span style="color:#996600;">&quot;Chore added.&quot;</span>, flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:message</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that it should send a message that chore creation failed&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    post <span style="color:#ff3333; font-weight:bold;">:create</span>, <span style="color:#ff3333; font-weight:bold;">:chore</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    assert_equal <span style="color:#996600;">&quot;Error saving Chore.&quot;</span>, flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:error</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>changes the create event to be</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> create
    <span style="color:#0066ff; font-weight:bold;">@chore</span> = Chore.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:chore</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@chore</span>.<span style="color:#9900CC;">save</span>
      flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:message</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Chore added.&quot;</span>
      redirect_to chores_url
    <span style="color:#9966CC; font-weight:bold;">else</span>
      flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:error</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Error saving Chore.&quot;</span>
      render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span>  <span style="color:#996600;">&quot;new&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Well that should be enough to move on with our cucumber feature and a solid beginning for our chore controller.  Running rake features again shows us that we indeed have passed the next step.  It is now looking for a field that isn&#8217;t there.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ cucumber features
Story:  Define chores  <span style="color: #666666; font-style: italic;"># features/define_chores.feature</span>
As a parent
I want to define chores
So that I can assign them to my children
  Scenario: Creating a chore                       <span style="color: #666666; font-style: italic;"># features/define_chores.feature:6</span>
    Given I am on the homepage                     <span style="color: #666666; font-style: italic;"># features/step_definitions/chores_steps.rb:1</span>
    When I follow <span style="color: #ff0000;">&quot;Add Chore&quot;</span>                      <span style="color: #666666; font-style: italic;"># features/step_definitions/webrat_steps.rb:8</span>
    And I fill <span style="color: #000000; font-weight: bold;">in</span> the chore with <span style="color: #ff0000;">&quot;My first chore&quot;</span>  <span style="color: #666666; font-style: italic;"># features/step_definitions/chores_steps.rb:5</span>
      Could not <span style="color: #c20cb9; font-weight: bold;">find</span> field labeled <span style="color: #ff0000;">&quot;chore[name]&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>RuntimeError<span style="color: #7a0874; font-weight: bold;">&#41;</span>
      c:<span style="color: #000000; font-weight: bold;">/</span>Ruby<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.8</span><span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>webrat-0.3.4<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>webrat<span style="color: #000000; font-weight: bold;">/</span>core<span style="color: #000000; font-weight: bold;">/</span>flunk.rb:<span style="color: #000000;">4</span>:<span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">'flunk'</span>
      c:<span style="color: #000000; font-weight: bold;">/</span>Ruby<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.8</span><span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>webrat-0.3.4<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>webrat<span style="color: #000000; font-weight: bold;">/</span>core<span style="color: #000000; font-weight: bold;">/</span>locators.rb:<span style="color: #000000;">16</span>:<span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">'field_labeled'</span>
      c:<span style="color: #000000; font-weight: bold;">/</span>Ruby<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.8</span><span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>webrat-0.3.4<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>webrat<span style="color: #000000; font-weight: bold;">/</span>core<span style="color: #000000; font-weight: bold;">/</span>locators.rb:<span style="color: #000000;">10</span>:<span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">'field'</span>
      c:<span style="color: #000000; font-weight: bold;">/</span>Ruby<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.8</span><span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>webrat-0.3.4<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>webrat<span style="color: #000000; font-weight: bold;">/</span>core<span style="color: #000000; font-weight: bold;">/</span>scope.rb:<span style="color: #000000;">183</span>:<span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">'locate_field'</span>
      c:<span style="color: #000000; font-weight: bold;">/</span>Ruby<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.8</span><span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>webrat-0.3.4<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>webrat<span style="color: #000000; font-weight: bold;">/</span>core<span style="color: #000000; font-weight: bold;">/</span>scope.rb:<span style="color: #000000;">41</span>:<span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">'fills_in'</span>
      c:<span style="color: #000000; font-weight: bold;">/</span>Ruby<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.8</span><span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>webrat-0.3.4<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>webrat<span style="color: #000000; font-weight: bold;">/</span>rails.rb:<span style="color: #000000;">88</span>:<span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">'send'</span>
      c:<span style="color: #000000; font-weight: bold;">/</span>Ruby<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.8</span><span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>webrat-0.3.4<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>webrat<span style="color: #000000; font-weight: bold;">/</span>rails.rb:<span style="color: #000000;">88</span>:<span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">'method_missing'</span>
      c:<span style="color: #000000; font-weight: bold;">/</span>Ruby<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.8</span><span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>actionpack-2.2.2<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>action_controller<span style="color: #000000; font-weight: bold;">/</span>integration.rb:<span style="color: #000000;">498</span>:<span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">'__send__'</span>
      c:<span style="color: #000000; font-weight: bold;">/</span>Ruby<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.8</span><span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>actionpack-2.2.2<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>action_controller<span style="color: #000000; font-weight: bold;">/</span>integration.rb:<span style="color: #000000;">498</span>:<span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">'method_missing'</span>
      .<span style="color: #000000; font-weight: bold;">/</span>features<span style="color: #000000; font-weight: bold;">/</span>step_definitions<span style="color: #000000; font-weight: bold;">/</span>chores_steps.rb:<span style="color: #000000;">6</span>:<span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">'And /^I fill in the chore with &quot;(.*)&quot;$/'</span>
      features<span style="color: #000000; font-weight: bold;">/</span>define_chores.feature:<span style="color: #000000;">9</span>:<span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span>And I fill <span style="color: #000000; font-weight: bold;">in</span> the chore with <span style="color: #ff0000;">&quot;My first chore&quot;</span><span style="color: #ff0000;">'
    And I press Add                                # features/step_definitions/chores_steps.rb:9
    Then I should see &quot;Chore added.&quot;               # features/step_definitions/webrat_steps.rb:83
    And I should see &quot;My first chore&quot;              # features/step_definitions/webrat_steps.rb:83
&nbsp;
&nbsp;
1 scenario
2 steps passed
1 step failed
3 steps skipped</span></pre></div></div>

<p>It appears that I had intended to name the field Chore.name, but when it came around time to name the field, I named it Chore.description.   I&#8217;ll go ahead and modify my step definition to read &#8216;chore[description]&#8216; and try again.</p>
<p>Much better this time.  Looks like the feature is looking for the flash message to be displayed on screen.  Currently we aren&#8217;t doing that, so I&#8217;ll need to drop that in.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">$ cucumber features
Story:  Define chores  <span style="color:#008000; font-style:italic;"># features/define_chores.feature</span>
As a parent
I want to define chores
So that I can assign them to my children
  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/chores_steps.rb:1</span>
    <span style="color:#9966CC; font-weight:bold;">When</span> I follow <span style="color:#996600;">&quot;Add Chore&quot;</span>                      <span style="color:#008000; font-style:italic;"># features/step_definitions/webrat_steps.rb:8</span>
    <span style="color:#9966CC; font-weight:bold;">And</span> I fill <span style="color:#9966CC; font-weight:bold;">in</span> the chore with <span style="color:#996600;">&quot;My first chore&quot;</span>  <span style="color:#008000; font-style:italic;"># features/step_definitions/chores_steps.rb:5</span>
    <span style="color:#9966CC; font-weight:bold;">And</span> I press Add                                <span style="color:#008000; font-style:italic;"># features/step_definitions/chores_steps.rb:9</span>
    <span style="color:#9966CC; font-weight:bold;">Then</span> I should see <span style="color:#996600;">&quot;Chore added.&quot;</span>               <span style="color:#008000; font-style:italic;"># features/step_definitions/webrat_steps.rb:83</span>
      expected: <span style="color:#006600; font-weight:bold;">/</span>Chore added.<span style="color:#006600; font-weight:bold;">/</span>m,
           got: <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span> <span style="color:#006600; font-weight:bold;">&#40;</span>using =~<span style="color:#006600; font-weight:bold;">&#41;</span>
      Diff:
      @@ <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span> <span style="color:#006600; font-weight:bold;">+</span><span style="color:#006666;">1</span>,<span style="color:#006666;">2</span> @@
      <span style="color:#006600; font-weight:bold;">-/</span>Chore added.<span style="color:#006600; font-weight:bold;">/</span>m
      <span style="color:#006600; font-weight:bold;">+</span><span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">Spec::Expectations::ExpectationNotMetError</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      .<span style="color:#006600; font-weight:bold;">/</span>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>:<span style="color:#006666;">84</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`Then /^I should see &quot;(.*)&quot;$/'
      features/define_chores.feature:11:in `</span><span style="color:#9966CC; font-weight:bold;">Then</span> I should see <span style="color:#996600;">&quot;Chore added.&quot;</span><span style="color:#996600;">'
    And I should see &quot;My first chore&quot;              # features/step_definitions/webrat_steps.rb:83
&nbsp;
&nbsp;
1 scenario
4 steps passed
1 step failed
1 step skipped</span></pre></div></div>

<p>Well, this isn&#8217;t beautiful but if I add this layout (as app/views/layout/application.html.erb), the flash will display and the feature step will pass and we can move on.</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
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
       &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&nbsp;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot;&gt;
&lt;head&gt;
  &lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html;charset=UTF-8&quot; /&gt;
&lt;title&gt;Chores
&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&nbsp;
&lt;%= flash[:error] %&gt;
&lt;%= flash[:message] %&gt;
&nbsp;
&lt;%= yield %&gt;
&nbsp;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>that passes our next step, and we are onto the step that says &#8216;And I should see &#8220;My first chore&#8221;&#8216;.  This means that we are expecting it to have redirected us to index and that the new record that we just added should display on screen.  Thus far I have placed nothing on the index view, so I will add the following.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#0066ff; font-weight:bold;">@chores</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>chore<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>=h chore.<span style="color:#9900CC;">description</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></td></tr></table></div>

<p>and now cucumber gives us some of it&#8217;s green cucumbery goodness with a passing test.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ cucumber features
Story:  Define chores  <span style="color: #666666; font-style: italic;"># features/define_chores.feature</span>
As a parent
I want to define chores
So that I can assign them to my children
  Scenario: Creating a chore                       <span style="color: #666666; font-style: italic;"># features/define_chores.feature:6</span>
    Given I am on the homepage                     <span style="color: #666666; font-style: italic;"># features/step_definitions/chores_steps.rb:1</span>
    When I follow <span style="color: #ff0000;">&quot;Add Chore&quot;</span>                      <span style="color: #666666; font-style: italic;"># features/step_definitions/webrat_steps.rb:8</span>
    And I fill <span style="color: #000000; font-weight: bold;">in</span> the chore with <span style="color: #ff0000;">&quot;My first chore&quot;</span>  <span style="color: #666666; font-style: italic;"># features/step_definitions/chores_steps.rb:5</span>
    And I press Add                                <span style="color: #666666; font-style: italic;"># features/step_definitions/chores_steps.rb:9</span>
    Then I should see <span style="color: #ff0000;">&quot;Chore added.&quot;</span>               <span style="color: #666666; font-style: italic;"># features/step_definitions/webrat_steps.rb:83</span>
    And I should see <span style="color: #ff0000;">&quot;My first chore&quot;</span>              <span style="color: #666666; font-style: italic;"># features/step_definitions/webrat_steps.rb:83</span>
&nbsp;
&nbsp;
<span style="color: #000000;">1</span> scenario
<span style="color: #000000;">6</span> steps passed</pre></div></div>

<p>Now I&#8217;ll commit to git and script/server to take a look at what our testing has created.  It looks good, once I remove index.html from the public directory, I can see the Add Chore link from the home page.  Clicking on it gives me a validated form that allows me to enter a chore entry and save it.   We also have tests that tell us that it&#8217;s all working well and functioning as expected.</p>
<p>We have taken a look at implementing a feature with Test::Unit, webrat and cucumber and it&#8217;s really not all that difficult to do.  Really the process from this point on is rinse, repeat, refactor.  I&#8217;m not sure at this point if I&#8217;ll return to post on Chores or not.  It&#8217;s been interesting documenting my process as I go along, but it&#8217;s also been a weight slowing me down.  In any case, I hope you have enjoyed this series on test-driven development and maybe have decided to try it out for yourself.  I hope so, and if you have kids, maybe check out chores when it&#8217;s done.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.rubyyot.com/2009/01/chores-a-test-driven-website-part-5/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Chores: a test driven website, part 4 (judgement day)</title>
		<link>http://blog.rubyyot.com/2009/01/chores-a-test-driven-website-part-4/</link>
		<comments>http://blog.rubyyot.com/2009/01/chores-a-test-driven-website-part-4/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 05:24:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Chores]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blog.rubyyot.com/?p=131</guid>
		<description><![CDATA[After you have a test running, it's time for refactoring. Refactoring being the process of improving code without changing it's functionality.  You can do this in a variety of ways, commonly you will want to do this to keep it DRY (remove duplication) and improve readability.]]></description>
			<content:encoded><![CDATA[<p><em><strong>Update</strong> This is part of <a href="http://blog.rubyyot.com/chores-a-test-driven-website/">the Chores series of posts</a></em></p>
<p>Back to building Chores, which started back <a href="http://blog.rubyyot.com/2009/01/chores-a-test-driven-website/" target="_blank">here</a> at the beginning.  After you have a test running, it&#8217;s time for refactoring.  <a href="http://www.refactoring.com/" target="_blank">Refactoring</a> being the process of improving code without changing it&#8217;s functionality.  You can do this in a variety of ways, commonly you will want to do this to keep it <a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself" target="_blank">DRY</a> (remove duplication) and improve readability.  Once you have refactored your application or your tests, you will want to run the tests to make sure that they are all still passing.</p>
<p>This is one of the great benefits of automated testing.  You can improve your code systematically and still be (for me, reasonably, since I&#8217;m still improving my testing skills and gaining confidence) sure that everything is still functioning as it was.</p>
<p>After a look through our code I didn&#8217;t see anything to change in the model and have modifed the unit test to read as follows.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'test_helper'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> ChoreShouldBeValidated <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::TestCase</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that a description is required&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    chore = Chore.<span style="color:#9900CC;">new</span>
    invalid chore
    chore.<span style="color:#9900CC;">description</span> = <span style="color:#996600;">&quot;Clean your room&quot;</span>
    valid chore
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that a description of 255 characters is valid&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    chore = Chore.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> max_description
    valid chore
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that a description of 256 characters is invalid&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    chore = Chore.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> max_description <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;8&quot;</span>
    invalid chore
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;the validation message for description&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    chore = Chore.<span style="color:#9900CC;">new</span>
    invalid chore
    assert_equal <span style="color:#996600;">&quot;Description cannot be blank and must be less than 255 characters long.&quot;</span>, chore.<span style="color:#9900CC;">errors</span>.<span style="color:#9900CC;">on</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:description</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  protected
    <span style="color:#9966CC; font-weight:bold;">def</span> max_description
      <span style="color:#996600;">&quot;This is a really long string that is the description of a chore and it is used to validate that the chore model will accept a string of 255 characters.  This string is longer than the longest string ever which also included some numbers 012345678901234567&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> invalid model
      assert !model.<span style="color:#9900CC;">valid</span>?
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> valid model
      assert model.<span style="color:#9900CC;">valid</span>?
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>You&#8217;ll notice the spec style specify rather than test block.  This can be achieved by simply adding the following to your test_helper file after the Test::Unit::TestCase class.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> specify <span style="color:#006600; font-weight:bold;">*</span>args, <span style="color:#006600; font-weight:bold;">&amp;</span>block
  test<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>I could move the valid and invalid methods to the test helper at this point as well, but so far there is no reason to so I am leaving it as it is, for now.</p>
<p>At this point I&#8217;m pretty happy with how it looks and reads.  It&#8217;s similar to a BDD spec, but is still using the standard Test::Unit.  However, looking back at this, I&#8217;m still not happy with the not null validation and the length validation being combined so I&#8217;m going to separate them out.</p>
<p>After making the changes and some refactors, my Chore model and matching test look like this:</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="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Chore <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  validates_length_of   <span style="color:#ff3333; font-weight:bold;">:description</span>,
                        <span style="color:#ff3333; font-weight:bold;">:maximum</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">255</span>,
                        <span style="color:#ff3333; font-weight:bold;">:allow_nil</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>,
                        <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Description cannot be longer than 255 characters.&quot;</span>
&nbsp;
  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:description</span>,
                        <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Description cannot be blank.&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>


<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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'test_helper'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> ChoreShouldBeValidated <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::TestCase</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that a description is required&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    chore = Chore.<span style="color:#9900CC;">new</span>
    invalid chore
    chore.<span style="color:#9900CC;">description</span> = <span style="color:#996600;">&quot;Clean your room&quot;</span>
    valid chore
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that a description of 255 characters is valid&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    chore = Chore.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> max_description
    valid chore
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;that a description of 256 characters is invalid&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    chore = Chore.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> overly_long_description
    invalid chore
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;message for a null description&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    chore = Chore.<span style="color:#9900CC;">new</span>
    validation_message_for chore, <span style="color:#ff3333; font-weight:bold;">:description</span>, <span style="color:#996600;">&quot;Description cannot be blank.&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  specify <span style="color:#996600;">&quot;message for an overly long description&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    chore = Chore.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> overly_long_description
    validation_message_for chore, <span style="color:#ff3333; font-weight:bold;">:description</span>, <span style="color:#996600;">&quot;Description cannot be longer than 255 characters.&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  protected
    <span style="color:#9966CC; font-weight:bold;">def</span> max_description
      <span style="color:#996600;">&quot;This is a really long string that is the description of a chore and it is used to validate that the chore model will accept a string of 255 characters.  This string is longer than the longest string ever which also included some numbers 012345678901234567&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> overly_long_description
      max_description <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;8&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> invalid model
      assert !model.<span style="color:#9900CC;">valid</span>?
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> valid model
      assert model.<span style="color:#9900CC;">valid</span>?
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> validation_message_for model, column, message
      invalid model
      assert_equal message, model.<span style="color:#9900CC;">errors</span>.<span style="color:#9900CC;">on</span><span style="color:#006600; font-weight:bold;">&#40;</span>column<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.rubyyot.com/2009/01/chores-a-test-driven-website-part-4/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Chores: A test driven website: part 3 (the revenge)</title>
		<link>http://blog.rubyyot.com/2009/01/chores-a-test-driven-website-part-3-the-revenge/</link>
		<comments>http://blog.rubyyot.com/2009/01/chores-a-test-driven-website-part-3-the-revenge/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 02:18:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Chores]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blog.rubyyot.com/?p=91</guid>
		<description><![CDATA[It's friday and the floors are piling up with dirty clothes, old homework and beds are unmade.  We need Chores, a Ruby on Rails website written with test driven principles in mind and created by the author of this article.]]></description>
			<content:encoded><![CDATA[<p><em><strong>Update</strong> This is part of <a href="http://blog.rubyyot.com/chores-a-test-driven-website/">the Chores series of posts</a></em></p>
<p>It&#8217;s friday; the floors are piling up with dirty clothes, old homework and beds are unmade.  We need Chores, a Ruby on Rails website written with test driven principles in mind and created by the author of this article.</p>
<p>This post is a continuation of <a href="http://blog.rubyyot.com/2009/01/chores-a-test-driven-website/" rel="me">parts 1</a> and <a href="http://blog.rubyyot.com/2009/01/chores-a-test-driven-website-part-2/" rel="me">2</a> in which I am documenting the steps with which I am creating a rails site and writing matching tests with Cucumber and Test::Unit.  When we left off last time I had found that I need a Chore model to continue.  At a minimum, we are going to need a description for the chore.  So let&#8217;s get to it:</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="bash" style="font-family:monospace;">$ ruby script<span style="color: #000000; font-weight: bold;">/</span>generate model Chore description:string
      exists  app<span style="color: #000000; font-weight: bold;">/</span>models<span style="color: #000000; font-weight: bold;">/</span>
      exists  test<span style="color: #000000; font-weight: bold;">/</span>unit<span style="color: #000000; font-weight: bold;">/</span>
      exists  test<span style="color: #000000; font-weight: bold;">/</span>fixtures<span style="color: #000000; font-weight: bold;">/</span>
      create  app<span style="color: #000000; font-weight: bold;">/</span>models<span style="color: #000000; font-weight: bold;">/</span>chore.rb
      create  test<span style="color: #000000; font-weight: bold;">/</span>unit<span style="color: #000000; font-weight: bold;">/</span>chore_test.rb
      create  test<span style="color: #000000; font-weight: bold;">/</span>fixtures<span style="color: #000000; font-weight: bold;">/</span>chores.yml
      create  db<span style="color: #000000; font-weight: bold;">/</span>migrate
      create  db<span style="color: #000000; font-weight: bold;">/</span>migrate<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">20090109221900</span>_create_chores.rb</pre></td></tr></table></div>

<p>Now I have a decision to make,  I know that I want to set the :limit and :null attributes in the migration file.  I can make the changes now and then write the tests on the matching validation or I can run the migration now and then write the tests on the validation and modify the columns in the db if necessary.  Because I am lazy and because I want to be sure the db constraints are in place I&#8217;m going to opt for the first option and then run my migration.</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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> CreateChores <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    create_table <span style="color:#ff3333; font-weight:bold;">:chores</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
      t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:description</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>, <span style="color:#ff3333; font-weight:bold;">:limit</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">255</span>
&nbsp;
      t.<span style="color:#9900CC;">timestamps</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    drop_table <span style="color:#ff3333; font-weight:bold;">:chores</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>and</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ rake db:migrate
==  CreateChores: migrating ===================================================
<span style="color: #660033;">--</span> create_table<span style="color: #7a0874; font-weight: bold;">&#40;</span>:chores<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   -<span style="color: #000000; font-weight: bold;">&gt;</span> 0.0160s
==  CreateChores: migrated <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.0160s<span style="color: #7a0874; font-weight: bold;">&#41;</span> ==========================================</pre></div></div>

<h2>Assert yourself!</h2>
<p>At the core of unit testing is the assertion.  An assertion is a statement that something will always be true.  In fact, if an assertion evaluates to false, the test will fail.  Test::Unit provides a wide variety of assertions ranging from a vanilla assert to the one that I find myself using most frequently, assert_equal.   There are also many more available for use in specific situations.</p>
<h2>Test!</h2>
<p>Now let&#8217;s write some tests to ensure our model validates the data.  First I&#8217;m going to make sure that the description is required and second I&#8217;m going to make sure that the description can be a maximum of 255 characters.  So lets open up the file ./test/unit/chore_test.rb</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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'test_helper'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> ChoreTest <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::TestCase</span>
  test <span style="color:#996600;">&quot;description is required&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    chore = Chore.<span style="color:#9900CC;">new</span>
    assert !chore.<span style="color:#9900CC;">valid</span>?
    chore.<span style="color:#9900CC;">description</span> = <span style="color:#996600;">&quot;Clean your room&quot;</span>
    assert chore.<span style="color:#9900CC;">valid</span>?
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  test <span style="color:#996600;">&quot;a description of 255 characters is valid&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    chore = Chore.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;This is a really long string that is the description of a chore and it is used to validate that the chore model will accept a string of 255 characters.  This string is longer than the longest string ever which also included some numbers 012345678901234567&quot;</span>
    assert chore.<span style="color:#9900CC;">valid</span>?
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  test <span style="color:#996600;">&quot;a description of 256 characters is invalid&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    chore = Chore.<span style="color:#9900CC;">new</span> <span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;This is a really long string that is the description of a chore and it is used to validate that the chore model will accept a string of 255 characters.  This string is longer than the longest string ever which also included some numbers 0123456789012345678&quot;</span>
    assert !chore.<span style="color:#9900CC;">valid</span>?
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>As you can see I have written 3 simple tests.  The first verifies that the description cannot be null, the second proves that a 255 character string is valid, and the third shows that a 256 character string is not valid. Lets run these tests and see what we get.</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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">$ rake test:units
<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#9966CC; font-weight:bold;">in</span> c:<span style="color:#006600; font-weight:bold;">/</span>rails<span style="color:#006600; font-weight:bold;">/</span>chores<span style="color:#006600; font-weight:bold;">&#41;</span>
c:<span style="color:#006600; font-weight:bold;">/</span>Ruby<span style="color:#006600; font-weight:bold;">/</span>bin<span style="color:#006600; font-weight:bold;">/</span>ruby <span style="color:#006600; font-weight:bold;">-</span>Ilib;test <span style="color:#996600;">&quot;c:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb&quot;</span> <span style="color:#996600;">&quot;test/unit/chore_test.rb&quot;</span>
Loaded suite 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>gems<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1.8</span><span style="color:#006600; font-weight:bold;">/</span>gems<span style="color:#006600; font-weight:bold;">/</span>rake<span style="color:#006600; font-weight:bold;">-</span>0.8.3<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>rake<span style="color:#006600; font-weight:bold;">/</span>rake_test_loader
&nbsp;
Started
.<span style="color:#9900CC;">FF</span>
Finished <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#006666;">0.75</span> seconds.
&nbsp;
  <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> Failure:
test_a_description_of_256_characters_is_invalid<span style="color:#006600; font-weight:bold;">&#40;</span>ChoreTest<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>test<span style="color:#006600; font-weight:bold;">/</span>unit<span style="color:#006600; font-weight:bold;">/</span>chore_test.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">18</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`test_a_description_of_256_characters_is_invalid'
     c:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:60:in `</span>__send__<span style="color:#996600;">'
     c:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:60:in `run'</span><span style="color:#006600; font-weight:bold;">&#93;</span>:
<span style="color:#006600; font-weight:bold;">&lt;</span>false<span style="color:#006600; font-weight:bold;">&gt;</span> is <span style="color:#9966CC; font-weight:bold;">not</span> <span style="color:#0000FF; font-weight:bold;">true</span>.
&nbsp;
  <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span> Failure:
test_description_is_required<span style="color:#006600; font-weight:bold;">&#40;</span>ChoreTest<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>test<span style="color:#006600; font-weight:bold;">/</span>unit<span style="color:#006600; font-weight:bold;">/</span>chore_test.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">6</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`test_description_is_required'
     c:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:60:in `</span>__send__<span style="color:#996600;">'
     c:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:60:in `run'</span><span style="color:#006600; font-weight:bold;">&#93;</span>:
<span style="color:#006600; font-weight:bold;">&lt;</span>false<span style="color:#006600; font-weight:bold;">&gt;</span> is <span style="color:#9966CC; font-weight:bold;">not</span> <span style="color:#0000FF; font-weight:bold;">true</span>.
&nbsp;
<span style="color:#006666;">3</span> tests, <span style="color:#006666;">3</span> assertions, <span style="color:#006666;">2</span> failures, <span style="color:#006666;">0</span> errors
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>c:<span style="color:#006600; font-weight:bold;">/</span>Ruby<span style="color:#006600; font-weight:bold;">/</span>bin<span style="color:#006600; font-weight:bold;">/</span>ruby <span style="color:#006600; font-weight:bold;">-</span>Ilib;test <span style="color:#996600;">&quot;c:/Ruby/lib/r...]
&nbsp;
(See full trace by running task with --trace)</span></pre></td></tr></table></div>

<p>What does this mean?  The summary tells us what happened in a nutshell.  At the bottom it says &#8220;3 tests&#8221;, that means that it ran 3 tests.  That is good because we wrote 3 tests.  Next, it says &#8220;3 assertions&#8221;, that means that in running the 3 tests, the testing framework encountered 3 assert statements.  But we wrote 4 assertions!  Why are there only 3 listed?  This is because, within a test, when the framework encounters a failed assertion, it stops running that test.  So we must have a test with an assertion that was not executed because another assertion failed before it was executed.</p>
<p> Moving on through the summary, it says &#8220;2 failures&#8221;.  This means that two of the things that we asserted to be true, were in fact false.  If we look in the details up above we see that the test to see if 255 characters was valid has passed, this makes sense since we don&#8217;t have any validation to limit the length at this point.  Finally it says &#8220;0 errors&#8221;, in this context you can think of errors as exceptions. Basically it means that the code that ran, ran without throwing an exception.</p>
<p>Now let&#8217;s make these tests pass.  The first failing test shows that the length of the description is not being limited to 255 characters.  So let&#8217;s do that.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Chore <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  validates_length_of <span style="color:#ff3333; font-weight:bold;">:description</span>, <span style="color:#ff3333; font-weight:bold;">:maximum</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">255</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>When we run our test we get:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">$ rake test:units
<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#9966CC; font-weight:bold;">in</span> c:<span style="color:#006600; font-weight:bold;">/</span>rails<span style="color:#006600; font-weight:bold;">/</span>chores<span style="color:#006600; font-weight:bold;">&#41;</span>
c:<span style="color:#006600; font-weight:bold;">/</span>Ruby<span style="color:#006600; font-weight:bold;">/</span>bin<span style="color:#006600; font-weight:bold;">/</span>ruby <span style="color:#006600; font-weight:bold;">-</span>Ilib;test <span style="color:#996600;">&quot;c:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb&quot;</span> <span style="color:#996600;">&quot;test/unit/chore_test.rb&quot;</span>
Loaded suite 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>gems<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1.8</span><span style="color:#006600; font-weight:bold;">/</span>gems<span style="color:#006600; font-weight:bold;">/</span>rake<span style="color:#006600; font-weight:bold;">-</span>0.8.3<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>rake<span style="color:#006600; font-weight:bold;">/</span>rake_test_loader
&nbsp;
Started
...
<span style="color:#9900CC;">Finished</span> <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#006666;">1.516</span> seconds.
&nbsp;
<span style="color:#006666;">3</span> tests, <span style="color:#006666;">4</span> assertions, <span style="color:#006666;">0</span> failures, <span style="color:#006666;">0</span> errors</pre></div></div>

<h2>Huh?</h2>
<p>But we never checked to see if the description was null!  Why are all of our tests passing?  If we look at the <a href="http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001894">api docs for validates_length_of</a> we will see that it does not allow nil by default, and that to allow nil you have to pass the symbol :allow_nil.   Well this is an interesting development, let&#8217;s make sure that the validation message is going to make sense by adding a test.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  test <span style="color:#996600;">&quot;validation message for description&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    chore = Chore.<span style="color:#9900CC;">new</span>
    chore.<span style="color:#9900CC;">valid</span>?
    assert_equal <span style="color:#996600;">&quot;Description cannot be blank and must be less than 255 characters long.&quot;</span>, chore.<span style="color:#9900CC;">errors</span>.<span style="color:#9900CC;">on</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:description</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">$ rake test:units
<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#9966CC; font-weight:bold;">in</span> c:<span style="color:#006600; font-weight:bold;">/</span>rails<span style="color:#006600; font-weight:bold;">/</span>chores<span style="color:#006600; font-weight:bold;">&#41;</span>
c:<span style="color:#006600; font-weight:bold;">/</span>Ruby<span style="color:#006600; font-weight:bold;">/</span>bin<span style="color:#006600; font-weight:bold;">/</span>ruby <span style="color:#006600; font-weight:bold;">-</span>Ilib;test <span style="color:#996600;">&quot;c:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb&quot;</span> <span style="color:#996600;">&quot;test/unit/chore_test.rb&quot;</span>
Loaded suite 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>gems<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1.8</span><span style="color:#006600; font-weight:bold;">/</span>gems<span style="color:#006600; font-weight:bold;">/</span>rake<span style="color:#006600; font-weight:bold;">-</span>0.8.3<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>rake<span style="color:#006600; font-weight:bold;">/</span>rake_test_loader
&nbsp;
Started
...<span style="color:#9900CC;">F</span>
Finished <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#006666;">0.781</span> seconds.
&nbsp;
  <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> Failure:
test_validation_message_for_description<span style="color:#006600; font-weight:bold;">&#40;</span>ChoreTest<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>test<span style="color:#006600; font-weight:bold;">/</span>unit<span style="color:#006600; font-weight:bold;">/</span>chore_test.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">24</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`test_validation_message_for_description'
     c:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:60:in `</span>__send__<span style="color:#996600;">'
     c:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:60:in `run'</span><span style="color:#006600; font-weight:bold;">&#93;</span>:
<span style="color:#006600; font-weight:bold;">&lt;</span><span style="color:#996600;">&quot;Description cannot be blank and must be less than 255 characters long.&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span> expected but was
<span style="color:#006600; font-weight:bold;">&lt;</span><span style="color:#996600;">&quot;is too long (maximum is 255 characters)&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>.
&nbsp;
<span style="color:#006666;">4</span> tests, <span style="color:#006666;">5</span> assertions, <span style="color:#006666;">1</span> failures, <span style="color:#006666;">0</span> errors
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>c:<span style="color:#006600; font-weight:bold;">/</span>Ruby<span style="color:#006600; font-weight:bold;">/</span>bin<span style="color:#006600; font-weight:bold;">/</span>ruby <span style="color:#006600; font-weight:bold;">-</span>Ilib;test <span style="color:#996600;">&quot;c:/Ruby/lib/r...]
&nbsp;
(See full trace by running task with --trace)</span></pre></div></div>

<p>Looks like it&#8217;s a good thing that we checked the error message.  The current message, &#8220;is too long (maximum is 255 characters)&#8221;,  would not have made any sense if the user had left the description nil.  Let&#8217;s update our model with the new message</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Chore <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  validates_length_of <span style="color:#ff3333; font-weight:bold;">:description</span>, <span style="color:#ff3333; font-weight:bold;">:maximum</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">255</span>, <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Description cannot be blank and must be less than 255 characters long.&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">$ rake test:units
<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#9966CC; font-weight:bold;">in</span> c:<span style="color:#006600; font-weight:bold;">/</span>rails<span style="color:#006600; font-weight:bold;">/</span>chores<span style="color:#006600; font-weight:bold;">&#41;</span>
c:<span style="color:#006600; font-weight:bold;">/</span>Ruby<span style="color:#006600; font-weight:bold;">/</span>bin<span style="color:#006600; font-weight:bold;">/</span>ruby <span style="color:#006600; font-weight:bold;">-</span>Ilib;test <span style="color:#996600;">&quot;c:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb&quot;</span> <span style="color:#996600;">&quot;test/unit/chore_test.rb&quot;</span>
Loaded suite 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>gems<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1.8</span><span style="color:#006600; font-weight:bold;">/</span>gems<span style="color:#006600; font-weight:bold;">/</span>rake<span style="color:#006600; font-weight:bold;">-</span>0.8.3<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>rake<span style="color:#006600; font-weight:bold;">/</span>rake_test_loader
&nbsp;
Started
....
<span style="color:#9900CC;">Finished</span> <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#006666;">0.797</span> seconds.
&nbsp;
<span style="color:#006666;">4</span> tests, <span style="color:#006666;">5</span> assertions, <span style="color:#006666;">0</span> failures, <span style="color:#006666;">0</span> errors</pre></div></div>

<p>Our unit tests are passing.  Let&#8217;s commit and then take a step back to see where we are.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ git status
<span style="color: #666666; font-style: italic;"># On branch master</span>
<span style="color: #666666; font-style: italic;"># Changed but not updated:</span>
<span style="color: #666666; font-style: italic;">#   (use &quot;git add &lt;file&gt;...&quot; to update what will be committed)</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#       modified:   config/routes.rb</span>
<span style="color: #666666; font-style: italic;">#       modified:   db/development.sqlite3</span>
<span style="color: #666666; font-style: italic;">#       modified:   db/test.sqlite3</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Untracked files:</span>
<span style="color: #666666; font-style: italic;">#   (use &quot;git add &lt;file&gt;...&quot; to include in what will be committed)</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#       app/controllers/chores_controller.rb</span>
<span style="color: #666666; font-style: italic;">#       app/controllers/home_controller.rb</span>
<span style="color: #666666; font-style: italic;">#       app/helpers/chores_helper.rb</span>
<span style="color: #666666; font-style: italic;">#       app/helpers/home_helper.rb</span>
<span style="color: #666666; font-style: italic;">#       app/models/</span>
<span style="color: #666666; font-style: italic;">#       app/views/</span>
<span style="color: #666666; font-style: italic;">#       db/migrate/</span>
<span style="color: #666666; font-style: italic;">#       db/schema.rb</span>
<span style="color: #666666; font-style: italic;">#       test/fixtures/</span>
<span style="color: #666666; font-style: italic;">#       test/functional/</span>
<span style="color: #666666; font-style: italic;">#       test/unit/</span>
no changes added to commit <span style="color: #7a0874; font-weight: bold;">&#40;</span>use <span style="color: #ff0000;">&quot;git add&quot;</span> and<span style="color: #000000; font-weight: bold;">/</span>or <span style="color: #ff0000;">&quot;git commit -a&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
$ git add .
$ git commit <span style="color: #660033;">-am</span> <span style="color: #ff0000;">&quot;Added the Chore model&quot;</span>
$ git push origin</pre></div></div>

<p>Well the easy way to see where we are is to re-run our feature with cucumber</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">$ cucumber features
Story:  Define chores  # features/define_chores.feature
As a parent
I want to define chores
So that I can assign them to my children
  Scenario: Creating a chore           # features/define_chores.feature:6
    Given I am on the homepage      # features/step_definitions/chores_steps.rb:1
    When I follow &quot;Add Chore&quot;         # features/step_definitions/webrat_steps.rb:8
      Called id for nil, which would mistakenly be 4 -- if you really wanted the
 id of nil, use object_id (ActionView::TemplateError)
      On line #1 of app/views/chores/new.html.erb
&nbsp;
      1: <span style="color:#006600; font-weight:bold;">&lt;%</span> form_for<span style="color:#006600; font-weight:bold;">&#40;</span>@chore<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
      2:   <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">error_messages</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
      3:
      4:   &lt;p&gt;</pre></div></div>

<p>So chore is nil in the view, but we have a model for it now.  The next step is to look back at what we&#8217;ve written and see if there are any opportunities for refactoring.  Then we will make sure that our controller is setting the @chore instance variable for us.  If you are interested in reading more about testing in Rails, here are some links for further reading:</p>
<ul>
<li><a href="http://www.nullislove.com/2007/11/10/testing-in-rails-introduction/">Null is Love >> Testing in Rails</a></li>
<li><a href="http://www.oreillynet.com/pub/a/ruby/2007/06/07/rails-testing-not-just-for-the-paranoid.html">Rails Testing: Not just for the Paranoid</a></li>
<li><a href="http://guides.rubyonrails.org/testing_rails_applications.html">A Guide to Testing Rails</a></li>
<li><a href="http://clarkware.com/presos/TestingRailsApps.pdf">Testing Rails Apps (presentation)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.rubyyot.com/2009/01/chores-a-test-driven-website-part-3-the-revenge/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Chores:  A test driven website, part 2</title>
		<link>http://blog.rubyyot.com/2009/01/chores-a-test-driven-website-part-2/</link>
		<comments>http://blog.rubyyot.com/2009/01/chores-a-test-driven-website-part-2/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 13:25:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Chores]]></category>
		<category><![CDATA[Introspection]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://blog.rubyyot.com/?p=53</guid>
		<description><![CDATA[This is part of the Chores series of posts
In which I continue to build the chores website and demonstating how simple it is to use a test first methodology. To follow along, please see my previous post.
Doh!  I forgot to commit!
When I left off, I had come to a natural stopping point.  A [...]]]></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>In which I continue to build the chores website and demonstating how simple it is to use a test first methodology. To follow along, please <a href="http://blog.rubyyot.com/2009/01/chores-a-test-driven-website/" rel="me">see my previous post.</a></p>
<h2>Doh!  I forgot to commit!</h2>
<p>When I left off, I had come to a natural stopping point.  A place where I was shifting focus from features to unit testing.  I like to make my commits when my tests are all passing, however if I&#8217;m implementing a new feature, I don&#8217;t apply this hueristic since completing a feature will span multiple units of work. So let&#8217;s commit this.  First let&#8217;s see where I am:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ git status
<span style="color: #666666; font-style: italic;"># On branch master</span>
<span style="color: #666666; font-style: italic;"># Untracked files:</span>
<span style="color: #666666; font-style: italic;">#   (use &quot;git add &lt;file&gt;...&quot; to include in what will be committed)</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#       features/</span>
<span style="color: #666666; font-style: italic;">#       lib/</span>
<span style="color: #666666; font-style: italic;">#       script/cucumber</span>
nothing added to commit but untracked files present <span style="color: #7a0874; font-weight: bold;">&#40;</span>use <span style="color: #ff0000;">&quot;git add&quot;</span> to track<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>Ok, git sees that I&#8217;ve added cucumber to my rails app and that I have added a feature.  Let&#8217;s add these changes to our commit.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ git add .</pre></div></div>

<p>and commit them</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ git commit <span style="color: #660033;">-m</span> <span style="color: #ff0000;">&quot;added feature to create chores&quot;</span></pre></div></div>

<p>and finally push a copy out to the repository I set up on my shared host.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ git push origin master</pre></div></div>

<p>there all set.</p>
<h2>Routing to home</h2>
<p>At this point I&#8217;m a little confused as to how to proceed test first.  We have a failing feature.  When I have a failing feature I like to start by making a test to simulate the same error.  However in this case, that is difficult at best, and the failing feature has already told me what I need to do, which is create a home_controller</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ ruby script<span style="color: #000000; font-weight: bold;">/</span>generate controller Home
      exists  app<span style="color: #000000; font-weight: bold;">/</span>controllers<span style="color: #000000; font-weight: bold;">/</span>
      exists  app<span style="color: #000000; font-weight: bold;">/</span>helpers<span style="color: #000000; font-weight: bold;">/</span>
      create  app<span style="color: #000000; font-weight: bold;">/</span>views<span style="color: #000000; font-weight: bold;">/</span>home
      exists  test<span style="color: #000000; font-weight: bold;">/</span>functional<span style="color: #000000; font-weight: bold;">/</span>
      create  app<span style="color: #000000; font-weight: bold;">/</span>controllers<span style="color: #000000; font-weight: bold;">/</span>home_controller.rb
      create  test<span style="color: #000000; font-weight: bold;">/</span>functional<span style="color: #000000; font-weight: bold;">/</span>home_controller_test.rb
      create  app<span style="color: #000000; font-weight: bold;">/</span>helpers<span style="color: #000000; font-weight: bold;">/</span>home_helper.rb</pre></div></div>

<p>.. and map a route to it.  To do that I open up my ./config/routes.rb file and add the following route.  I like to get rid of the legacy routes so I&#8217;ll replace the entire text of the file with the following</p>
<p>[sourcecode language="ruby"]<br />
ActionController::Routing::Routes.draw do |map|<br />
  map.root :controller => &#8216;home&#8217;<br />
end
</pre>
<h2>So what are these controllers and routes?</h2>
<p>If you are new to Rails, you are probably wondering what we just did.  The controller is the C in the MVC pattern that Rails uses.  It's role is to handle web serving of the application.  It's kind of like an office assistant for your application.  It be able to handle frequent requests and delegate them to the proper subject matter expert.  When a request comes in for a list of Widgets, it will go to the Widget model and ask it for a list of widgets and then pass that off to the user interface (view) to display in the browser.  When the user interface passes it a bag of information and asks it to save the information, the controller will create an instance of the appropriate model from the bag of information and then tell the model it should save the information.</p>
<p>Controllers really shouldn't become too involved in the details, but should delegate well to the objects that know what they are doing.  Rails have a nice script to generate controllers, which we used above.  In this case we generated a home controller which we will use to handle requests for the application's home page or root.</p>
<p>The second thing we did is define a route.  Routes are related to controllers, in that they show the framework how to translate a request such as http://www.exmaple.com/sprocket/new into a method call on a controller.  In the case of the above url, it would call the method new on the sprocket controller.  You can find your routes in config/routes.rb and view all of your current routes with the rake task</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">rake routes</pre></div></div>

<h2>On with the show!</h2>
<p>Now let's go back to cucumber and see where we are.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ cucumber features
Story:  Define chores  <span style="color: #666666; font-style: italic;"># features/define_chores.feature</span>
As a parent
I want to define chores
So that I can assign them to my children
  Scenario: Creating a chore       <span style="color: #666666; font-style: italic;"># features/define_chores.feature:6</span>
    Given I am on the homepage    <span style="color: #666666; font-style: italic;"># features/step_definitions/chores_steps.rb:1</span>
      No action responded to index. Actions:  <span style="color: #7a0874; font-weight: bold;">&#40;</span>ActionController::UnknownAction<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>Ok, our route is now wired up nicely to the controller, but the home controller does not respond to index.  Lets add this to the controller</p>
<p>[sourcecode language="ruby"]<br />
class HomeController < ApplicationController<br />
  def index</p>
<p>  end<br />
end
</pre>
<p>and try cucumber again</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ cucumber features
Story:  Define chores  <span style="color: #666666; font-style: italic;"># features/define_chores.feature</span>
As a parent
I want to define chores
So that I can assign them to my children
  Scenario: Creating a chore     <span style="color: #666666; font-style: italic;"># features/define_chores.feature:6</span>
    Given I am on the homepage   <span style="color: #666666; font-style: italic;"># features/step_definitions/chores_steps.rb:1</span>
      Missing template home<span style="color: #000000; font-weight: bold;">/</span>index.erb <span style="color: #000000; font-weight: bold;">in</span> view path c:<span style="color: #000000; font-weight: bold;">/</span>rails<span style="color: #000000; font-weight: bold;">/</span>chores<span style="color: #000000; font-weight: bold;">/</span>app<span style="color: #000000; font-weight: bold;">/</span>views: <span style="color: #7a0874; font-weight: bold;">&#40;</span>ActionView::MissingTemplate<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>Ah ha!  We are making progress.  Our request has passed through the controller and is now looking for a view to render. One thing that I would like to point out is that doing the development in this way really lays out how the pieces of Rails all fit together.  Something that we wouldn't have seen if we had chosen to generate a scaffold or we built it piecemeal without the tests metering our pace.</p>
<h2></h2>
<h2>Views</h2>
<p>Views are what end up being displayed in the web browser, they are a sort of template.  By default, erb will process the template in the context of your request and output the dynamic content.  Pretty cool stuff, and it gives you great control over your output.</p>
<p>Well what are we waiting for, let's make a view.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">touch</span> app<span style="color: #000000; font-weight: bold;">/</span>views<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>index.html.erb</pre></div></div>

<p>In windows you can use your IDE or folder browser to create the file.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ cucumber features
Story:  Define chores  <span style="color: #666666; font-style: italic;"># features/define_chores.feature</span>
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: #666666; font-style: italic;"># features/define_chores.feature:6</span>
    Given I am on the homepage                        <span style="color: #666666; font-style: italic;"># features/step_definitions/webrat_steps.rb:6</span>
    When I follow <span style="color: #ff0000;">&quot;Add Chore&quot;</span>                         <span style="color: #666666; font-style: italic;"># features/step_definitions/webrat_steps.rb:18</span>
      Could not <span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #c20cb9; font-weight: bold;">link</span> with text or title or <span style="color: #c20cb9; font-weight: bold;">id</span> <span style="color: #ff0000;">&quot;Add Chore&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>Webrat::NotFoundError<span style="color: #7a0874; font-weight: bold;">&#41;</span>
      <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">eval</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>:<span style="color: #000000;">2</span>:<span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">'/^I follow &quot;([^&quot;]*)&quot;$/'</span>
      features<span style="color: #000000; font-weight: bold;">/</span>define_chores.feature:<span style="color: #000000;">8</span>:<span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">'When I follow &quot;Add Chore&quot;'</span>
    And I fill <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">&quot;chore[name]&quot;</span> with <span style="color: #ff0000;">&quot;My first chore&quot;</span> <span style="color: #666666; font-style: italic;"># features/step_definitions/webrat_steps.rb:22</span>
    And I press <span style="color: #ff0000;">&quot;Add&quot;</span>                                 <span style="color: #666666; font-style: italic;"># features/step_definitions/webrat_steps.rb:14</span>
    Then I should see <span style="color: #ff0000;">&quot;Chore added.&quot;</span>                  <span style="color: #666666; font-style: italic;"># features/step_definitions/webrat_steps.rb:93</span>
    And I should see <span style="color: #ff0000;">&quot;My first chore&quot;</span>                 <span style="color: #666666; font-style: italic;"># features/step_definitions/webrat_steps.rb:93</span>
&nbsp;
<span style="color: #000000;">1</span> scenario
<span style="color: #000000;">1</span> failed step
<span style="color: #000000;">4</span> skipped steps
<span style="color: #000000;">1</span> passed step
rake aborted<span style="color: #000000; font-weight: bold;">!</span></pre></div></div>

<p>Hey look! Our first step has passed!  Let's give ourselves a pat on the back.  We have a website with a dynamic (albeit blank) homepage, that is tested.</p>
<h2>Not all quotes are equal</h2>
<p>I had an issue with my feature where the double quotes in my feature were translated automatically to open and closed style quotes.  This was causing my steps not to match.  If you find that you have a bunch of steps that are undefined at this point, that may be the issue.</p>
<h2>Catch the passing step fever</h2>
<p>The next step is one that is generated by webrat.  It is looking for a link to follow called "Add Chore".  The problem is that we didn't actually add the link, or anything else to the view. So let's update the view to read</p>
<p>[sourcecode language="ruby"]<br />
<%= link_to "Add Chore", chore_path %>
</pre>
<p>and fire up cucumber</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ cucumber features
Story:  Define chores  <span style="color: #666666; font-style: italic;"># features/define_chores.feature</span>
As a parent
I want to define chores
So that I can assign them to my children
  Scenario: Creating a chore      <span style="color: #666666; font-style: italic;"># features/define_chores.feature:6</span>
    Given I am on the homepage    <span style="color: #666666; font-style: italic;"># features/step_definitions/chores_steps.rb:1</span>
      undefined <span style="color: #7a0874; font-weight: bold;">local</span> variable or method <span style="color: #ff0000;">'chore_path'</span> <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #666666; font-style: italic;">#&lt;actionView::Base:0x4baf67c&gt; (ActionView::TemplateError)</span>
      On line <span style="color: #666666; font-style: italic;">#1 of app/views/home/index.html.erb</span>
&nbsp;
      <span style="color: #000000;">1</span>: <span style="color: #000000; font-weight: bold;">&lt;%</span>= link_to <span style="color: #ff0000;">&quot;Add Chore&quot;</span>, chore_path <span style="color: #000000; font-weight: bold;">%&gt;</span>
&nbsp;
      app<span style="color: #000000; font-weight: bold;">/</span>views<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>index.html.erb:<span style="color: #000000;">1</span></pre></div></div>

<p>ok, we need to define resource for this, let's add a route and controller.</p>
<p>[sourcecode language="ruby"]<br />
ActionController::Routing::Routes.draw do |map|<br />
  map.resources :chores<br />
  map.root :controller => 'home'<br />
end
</pre>
<p>and</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ ruby script<span style="color: #000000; font-weight: bold;">/</span>generate controller Chores
      exists  app<span style="color: #000000; font-weight: bold;">/</span>controllers<span style="color: #000000; font-weight: bold;">/</span>
      exists  app<span style="color: #000000; font-weight: bold;">/</span>helpers<span style="color: #000000; font-weight: bold;">/</span>
      create  app<span style="color: #000000; font-weight: bold;">/</span>views<span style="color: #000000; font-weight: bold;">/</span>chores
      exists  test<span style="color: #000000; font-weight: bold;">/</span>functional<span style="color: #000000; font-weight: bold;">/</span>
      create  app<span style="color: #000000; font-weight: bold;">/</span>controllers<span style="color: #000000; font-weight: bold;">/</span>chores_controller.rb
      create  test<span style="color: #000000; font-weight: bold;">/</span>functional<span style="color: #000000; font-weight: bold;">/</span>chores_controller_test.rb
      create  app<span style="color: #000000; font-weight: bold;">/</span>helpers<span style="color: #000000; font-weight: bold;">/</span>chores_helper.rb</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ cucumber features
Story:  Define chores  <span style="color: #666666; font-style: italic;"># features/define_chores.feature</span>
As a parent
I want to define chores
So that I can assign them to my children
  Scenario: Creating a chore    <span style="color: #666666; font-style: italic;"># features/define_chores.feature:6</span>
    Given I am on the homepage   <span style="color: #666666; font-style: italic;"># features/step_definitions/chores_steps.rb:1</span>
      chore_url failed to generate from <span style="color: #7a0874; font-weight: bold;">&#123;</span>:<span style="color: #007800;">action</span>=<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #ff0000;">&quot;show&quot;</span>, :<span style="color: #007800;">controller</span>=<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #ff0000;">&quot;chores&quot;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>
 - you may have ambiguous routes, or you may need to supply additional parameter
s <span style="color: #000000; font-weight: bold;">for</span> this route.  content_url has the following required parameters: <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #ff0000;">&quot;chores&quot;</span>,
 :<span style="color: #c20cb9; font-weight: bold;">id</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> - are they all satisfied? <span style="color: #7a0874; font-weight: bold;">&#40;</span>ActionView::TemplateError<span style="color: #7a0874; font-weight: bold;">&#41;</span>
      On line <span style="color: #666666; font-style: italic;">#1 of app/views/home/index.html.erb</span>
&nbsp;
      <span style="color: #000000;">1</span>: <span style="color: #000000; font-weight: bold;">&lt;%</span>= link_to <span style="color: #ff0000;">&quot;Add Chore&quot;</span>, chore_path <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<p>Dang it! still getting an error.  Hmmm.. wait a minute.  chore_path is generating a show action.  That's not what we want to do.  We want a new action to be RESTful or maybe an index action if we don't see REST in the future for this app. A quick consultation with the domain expert (me) says that there is no valid reason not follow the RESTful guidelines at this point so let's fix the home/index view to read.</p>
<p>[sourcecode language="ruby"]<br />
<%= link_to "Add Chore", new_chore_path %>
</pre>
<p>cucumber says...</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ cucumber features
Story:  Define chores  <span style="color: #666666; font-style: italic;"># features/define_chores.feature</span>
As a parent
I want to define chores
So that I can assign them to my children
  Scenario: Creating a chore   <span style="color: #666666; font-style: italic;"># features/define_chores.feature:6</span>
    Given I am on the homepage   <span style="color: #666666; font-style: italic;"># features/step_definitions/chores_steps.rb:1</span>
    When I follow <span style="color: #ff0000;">&quot;Add Chore&quot;</span>   <span style="color: #666666; font-style: italic;"># features/step_definitions/webrat_steps.rb:8</span>
      No action responded to new. Actions:  <span style="color: #7a0874; font-weight: bold;">&#40;</span>ActionController::UnknownAction<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>Great!  That fixed it.  Now we need to make sure there is a new action on the chores controller.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ChoresController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  <span style="color:#9966CC; font-weight:bold;">def</span> new
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ cucumber features
Story:  Define chores  <span style="color: #666666; font-style: italic;"># features/define_chores.feature</span>
As a parent
I want to define chores
So that I can assign them to my children
  Scenario: Creating a chore    <span style="color: #666666; font-style: italic;"># features/define_chores.feature:6</span>
    Given I am on the homepage    <span style="color: #666666; font-style: italic;"># features/step_definitions/chores_steps.rb:1</span>
    When I follow <span style="color: #ff0000;">&quot;Add Chore&quot;</span>      <span style="color: #666666; font-style: italic;"># features/step_definitions/webrat_steps.rb:8</span>
      Missing template chores<span style="color: #000000; font-weight: bold;">/</span>new.erb <span style="color: #000000; font-weight: bold;">in</span> view path c:<span style="color: #000000; font-weight: bold;">/</span>rails<span style="color: #000000; font-weight: bold;">/</span>chores<span style="color: #000000; font-weight: bold;">/</span>app<span style="color: #000000; font-weight: bold;">/</span>views: <span style="color: #7a0874; font-weight: bold;">&#40;</span>ActionView::MissingTemplate<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>cucumber likes it!  But we still have an error.  Let's create ./app/views/chores/new.html.erb as:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span> form_for<span style="color:#006600; font-weight:bold;">&#40;</span>@chore<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">error_messages</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
&nbsp;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">submit</span> <span style="color:#996600;">&quot;Add&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Hmmm.. wait we are getting ahead of ourselves. We need a model to be able to pass it to our view.  We will have to generate that next.</p>
<p>Well we didn't get to Test::Unit this time around, but with a model in sight it's sure to be right around the corner.  Until next time..</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.rubyyot.com/2009/01/chores-a-test-driven-website-part-2/feed/</wfw:commentRss>
		<slash:comments>8</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>
	</channel>
</rss>
