<?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>Clayton Lengel-Zigich &#187; Uncategorized</title>
	<atom:link href="http://www.claytonlz.com/index.php/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.claytonlz.com</link>
	<description>Ruby on Rails Developer</description>
	<lastBuildDate>Thu, 15 Jul 2010 07:44:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using Ruby&#8217;s true in Cucumber Multiline Tables</title>
		<link>http://www.claytonlz.com/index.php/2009/06/using-rubys-true-in-cucumber-multiline-tables/</link>
		<comments>http://www.claytonlz.com/index.php/2009/06/using-rubys-true-in-cucumber-multiline-tables/#comments</comments>
		<pubDate>Sat, 06 Jun 2009 04:02:39 +0000</pubDate>
		<dc:creator>Clayton</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.claytonlz.com/?p=266</guid>
		<description><![CDATA[Last week my pair and I ran into a problem with a failing Cucumber scenario. We were using one of the more awesome features of Cucumber, multiline tables, when we got the unexpected failure. We realized that what we were doing was probably a common pattern and would certain trip other's up.]]></description>
			<content:encoded><![CDATA[<p></p><p>Last week my pair and I ran into a problem with a failing Cucumber scenario. We were using one of the more awesome features of Cucumber, multiline tables, when we got the unexpected failure. We realized that what we were doing was probably a common pattern and would certain trip other&#8217;s up.</p>

<h3>Scenario and Step</h3>

<p>If you&#8217;re not familiar with multiline tables in cucumber, take a look at the <a href="http://wiki.github.com/aslakhellesoy/cucumber/multiline-step-arguments">wiki entry</a>.  We were specifying a list of attributes on one of our models that we wanted to have access to in our step. First, here is an example scenario outline.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">Scenario: Finding a specific dog
  Given an existing microchipped dog named <span style="color:#996600;">&quot;George&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">When</span> I search <span style="color:#9966CC; font-weight:bold;">for</span> a dog with the following attributes:
    <span style="color:#006600; font-weight:bold;">|</span> name   <span style="color:#006600; font-weight:bold;">|</span> microchipped <span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#006600; font-weight:bold;">|</span> George <span style="color:#006600; font-weight:bold;">|</span> <span style="color:#0000FF; font-weight:bold;">true</span>         <span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#9966CC; font-weight:bold;">Then</span> I find <span style="color:#996600;">&quot;1&quot;</span> dog
  <span style="color:#9966CC; font-weight:bold;">And</span> that dog should be named <span style="color:#996600;">&quot;George&quot;</span></pre></td></tr></table></div>




<p>Our step was taking the multiline table attributes and using them in an ActiveRecord find, like this.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">/</span>^I search <span style="color:#9966CC; font-weight:bold;">for</span> a dog with the following attributes:$<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>table<span style="color:#006600; font-weight:bold;">|</span>
  table.<span style="color:#9900CC;">hashes</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>attributes<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#0066ff; font-weight:bold;">@dog</span> = Dog.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:first</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> attributes<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>In our example we are looking for two attributes a string called &#8220;name&#8221; and a boolean called &#8220;microchipped&#8221;. However, this find will always fail, even if your &#8216;Given an existing microchipped dog named &#8220;George&#8221;&#8216; step explicitly sets up the correct object in the beginning of the test.</p>

<h3>The Fix</h3>

<p>We found that if we changed our scenario to look like this, everything worked fine.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">Scenario: Finding a specific dog
  Given an existing microchipped dog named <span style="color:#996600;">&quot;George&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">When</span> I search <span style="color:#9966CC; font-weight:bold;">for</span> a dog with the following attributes:
    <span style="color:#006600; font-weight:bold;">|</span> name   <span style="color:#006600; font-weight:bold;">|</span> microchipped <span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#006600; font-weight:bold;">|</span> George <span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006666;">1</span>            <span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#9966CC; font-weight:bold;">Then</span> I find <span style="color:#996600;">&quot;1&quot;</span> dog
  <span style="color:#9966CC; font-weight:bold;">And</span> that dog should be named <span style="color:#996600;">&quot;George&quot;</span></pre></td></tr></table></div>




<p>See the change? We changed &#8220;true&#8221; for microchipped to &#8220;1&#8243;. Typically when you&#8217;re working with rails you can pass <tt>true</tt> in the conditions of an ActiveRecord find and Rails will convert that to the correct <span class="caps">SQL </span>string for you. However, because cucumber passes each argument in the multiline table rows as strings, Rails never has the chance to covert that <tt>true</tt> for you.</p>

<p>In the first, failing, example you end up with something like this for your <span class="caps">SQL</span>:</p>


<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> dogs <span style="color: #993333; font-weight: bold;">WHERE</span> name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;George&quot;</span> <span style="color: #993333; font-weight: bold;">AND</span> microchipped <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #993333; font-weight: bold;">LIMIT</span> <span style="color: #cc66cc;">1</span>;</pre></div></div>




<p>In the second, passing, example you end up with something like this:</p>


<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> dogs <span style="color: #993333; font-weight: bold;">WHERE</span> name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;George&quot;</span> <span style="color: #993333; font-weight: bold;">AND</span> microchipped <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #993333; font-weight: bold;">LIMIT</span> <span style="color: #cc66cc;">1</span>;</pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.claytonlz.com/index.php/2009/06/using-rubys-true-in-cucumber-multiline-tables/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
