<?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>/dev/caffeine &#187; testing</title>
	<atom:link href="http://devcaffeine.com/tags/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://devcaffeine.com</link>
	<description>anything worth taking seriously is worth making fun of</description>
	<lastBuildDate>Thu, 24 Sep 2009 20:50:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Spec a FormBuilder in RSpec</title>
		<link>http://devcaffeine.com/2009/02/06/spec-a-formbuilder-in-rspec/</link>
		<comments>http://devcaffeine.com/2009/02/06/spec-a-formbuilder-in-rspec/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 16:04:31 +0000</pubDate>
		<dc:creator>cflipse</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://devcaffeine.com/?p=62</guid>
		<description><![CDATA[yesterday, I started trying to work on a helper for generating a frequently-repeated calendar widget for the rails app at $WORK.  Being the good BDDer that I am, I launched off a new spec to test the form builder &#8212; and to attempt to backfill specs and cover up the evidence of my naughty [...]]]></description>
			<content:encoded><![CDATA[<p>yesterday, I started trying to work on a helper for generating a frequently-repeated calendar widget for the rails app at $WORK.  Being the good <abbr title="behavior driven development">BDD</abbr>er that I am, I launched off a new spec to test the form builder &#8212; and to attempt to backfill specs and cover up the evidence of my naughty non-testing past.</p>
<p>This is a form builder we use in a couple of heavily used forms on the site, so we &#8220;know&#8221; this works, aside from the new code I want to add.  But in moving in to backfill some specs, I discovered that, like many things relating to form builders in rails, it&#8217;s not the easiest thing to do, nor is it exactly well documented &#8230;</p>
<p>After a bit of digging, I finally managed to work out the right setup to get this going.  Documented below for posterity</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># spec/helper/random_form_builder_spec.rb</span>
  describe RandomFormBuilder <span style="color:#9966CC; font-weight:bold;">do</span>
    attr_reader <span style="color:#ff3333; font-weight:bold;">:builder</span>, <span style="color:#ff3333; font-weight:bold;">:helper</span>
    before <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#0066ff; font-weight:bold;">@helper</span> = <span style="color:#CC00FF; font-weight:bold;">Object</span>.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">ActionView::Helpers::FormHelper</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@object</span> = stub_model<span style="color:#006600; font-weight:bold;">&#40;</span>Random<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@builder</span> = RandomFormBuilder.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:random</span>, <span style="color:#0066ff; font-weight:bold;">@object</span>, <span style="color:#0066ff; font-weight:bold;">@helper</span>, <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>You can then spec as normal, calling methods on builder and verifying the result.</p>
<p>The tricky part is the constructor.</p>
<dl>
<dt>:random</dt>
<dd>This is the name of the object, by analog, the first argument of a  <tt>#form_for</tt></dd>
<dt>@object</dt>
<dd>The object you run with; where the form pulls field values from</dd>
<dt>@helper</dt>
<dd>this is the tricky one.  A form builder is really just a pass-through, decorating calls down to methods that live in FormHelper.  If you don&#8217;t have a template that defines the FormHelper methods, failures abound.</p>
<dt>{}</dt>
</dd>
<dd>Options for the FormBuilder.  Here, unused</dd>
<dt>nil</dt>
<dd>A proc</dd>
</dl>
]]></content:encoded>
			<wfw:commentRss>http://devcaffeine.com/2009/02/06/spec-a-formbuilder-in-rspec/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
