<?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; Testing</title>
	<atom:link href="http://www.claytonlz.com/index.php/category/testing/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>Zero To Tested With Cucumber and Factory Girl</title>
		<link>http://www.claytonlz.com/index.php/2010/03/zero-to-tested-with-cucumber-and-factory-girl/</link>
		<comments>http://www.claytonlz.com/index.php/2010/03/zero-to-tested-with-cucumber-and-factory-girl/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 17:30:35 +0000</pubDate>
		<dc:creator>Clayton</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://www.claytonlz.com/?p=381</guid>
		<description><![CDATA[Get your project from zero to tested leveraging the power of Cucumber and Factory Girl Testing can be a daunting task, especially for developers who are new to Rails. It sounds like a great idea, but where do you start? What do you test? What don&#8217;t you test? For many developers, the hardest part of [...]]]></description>
			<content:encoded><![CDATA[<p></p><h3>Get your project from zero to tested leveraging the power of Cucumber and Factory Girl</h3>

<p>Testing can be a daunting task, especially for developers who are new to Rails. It sounds like a great idea, but where do you start? What do you test? What don&#8217;t you test? For many developers, the hardest part of testing is jumping in and getting your feet wet. Luckily, Cucumber makes getting started with testing easy, you&#8217;ll be testing your code form top to bottom in no time.</p>

<h4>What is Cucumber?</h4>

<p>Cucumber, in simple terms, is a framework for writing human-readable, meaningful tests that allow you to test the full functionality of your Rails project from database access to business logic to what&#8217;s displayed in your views.</p>

<p>From the <a href="http://cukes.info">Cucumber Wiki</a>:</p>

<blockquote><p>Cucumber lets software development teams describe how software should behave in plain text. The text is written in a business-readable domain-specific language and serves as documentation, automated tests and development-aid &#8211; all rolled into one format.</p></blockquote>

<p>When you write your tests with Cucumber you get full-stack automated tests, documentation and a tool to help you communicate with your product owner.</p>

<h4>Who&#8217;s the Factory Girl?</h4>

<p><a href="http://robots.thoughtbot.com/post/159807023/waiting-for-a-factory-girl">Factory Girl</a> is a fixture replacement for Rails that allows you to create meaningful objects for use in your tests.</p>

<p>From the <a href="http://github.com/thoughtbot/factory_girl">Factory Girl Github Page</a>:</p>

<blockquote><p>factory_girl is a fixtures replacement with a straightforward definition syntax, support for multiple build strategies (saved instances, unsaved instances, attribute hashes, and stubbed objects), and support for multiple factories for the same class (user, admin_user, and so on), including factory inheritance.</p></blockquote>

<p>When you use Factory Girl in conjunction with Cucumber you can simplify complex test setup and use real objects from the database, not brittle mocks.</p>

<h4>Getting Setup</h4>

<p>Getting your project setup with Cucumber and Factory Girl is pretty straightforward. First, you&#8217;ll need a few gems installed:</p>



<pre lang="">

    sudo gem install cucumber-rails
    sudo gem install webrat
    sudo gem install thoughtbot-factory_girl --source=http://gemcutter.org

</pre>



<p class="note">Note: As of 0.5.0 the Rails specific functionality was extracted from the cucumber gem into the cucumber-rails gem</p>

<p>Cucumber comes pre-packaged with some generators for getting your project ready for testing. From the root of your Rails project, run the generator:</p>



<pre>

    ruby script/generate cucumber --webrat --rspec

</pre>



<p>You&#8217;ll notice that we&#8217;re passing a couple of flags to the generator, <code>--webrat</code> specifies that we want to use webrat as our automated browser and <code>--rspec</code> specifies that we want to use rspec as our test framework. If you don&#8217;t pass these options Cucumber will guess which options you want based on the gems which you have installed.</p>

<p>After running the generator, you will see some output about the files that were created:</p>


<pre>

    create  config/cucumber.yml
    create  config/environments/cucumber.rb
    create  script/cucumber
    create  features/step_definitions
    create  features/step_definitions/web_steps.rb
    create  features/support
    create  features/support/env.rb
    create  features/support/paths.rb
    create  lib/tasks
    create  lib/tasks/cucumber.rake

</pre>



<p>Here&#8217;s a brief description of what some of the important files do:</p>

<p>*<tt>config/cucumber.rb</tt>*: This is your cucumber environment, similar to <code>production.rb</code> or <code>development.rb</code>. Typically you&#8217;ll add cucumber specific <code>config.gem</code> directives and similar &#8220;cucumber only&#8221; items in here.</p>

<p>*<tt>features/step_definitions/web_steps.rb</tt>*: These are the webrat steps that you get for free with Cucumber and Webrat.</p>

<p>*<tt>features/support/env.rb</tt>*: This file has Cucumber specific setup and configuration options. This changes often, so it&#8217;s recommended to not alter this file directly, but to create your own custom <code>env.rb</code> (e.g. <code>custom_env.rb</code>).</p>

<p>*<tt>features/support/paths.rb</tt>*: Cucumber needs to be aware of the custom routes in your application that you plan on using when testing, this is where they are specified.</p>

<h4>Cucumber from 10,000ft</h4>

<p>Now that you&#8217;ve got your project configured to use Cucumber, let&#8217;s go over a high level view of the general concept of testing with Cucumber.</p>

<p>Cucumber uses &#8220;Features&#8221; to describe the functionality of your application. Features, stored in the <code>features</code> directory, are written in a simple human-readable format called <a href="http://wiki.github.com/aslakhellesoy/cucumber/gherkin">Gherkin</a>. The &#8220;Steps&#8221; in your feature files that describe the functionality of your application are backed up by step files in the <code>features/step_definitions</code> directory. The step files act like translations between the human-readable feature files and the underlying Ruby code of your Rails application.</p>

<p>Typically you will be using Cucumber in conjunction with Webrat to test your application. Webrat acts as the person behind the keyboard clicking around the site, filling out forms and viewing resulting pages. If you&#8217;ve ever tested your project by manually filling out forms and verifying output, that&#8217;s exactly what Webrat does for you, except it&#8217;s automated.</p>

<h4>Let&#8217;s Get Testing Already</h4>

<p>Rather than go into detail about <a href="http://en.wikipedia.org/wiki/Test-driven_development"><span class="caps">TDD</span></a> or <a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development"><span class="caps">BDD</span></a> and why you should use them, I&#8217;m going to assume that you&#8217;re like most Rails developers looking to learn more about testing. You probably have an application or two out there in the wild, seemingly working just fine. However, you know that you should get some test coverage in there so that you can confidently make changes and debug problems as they come up. With that in mind, I am going to use a sample application that already has some functionality and we will add our Cucumber tests where needed.</p>

<h4>A Veterinary Patient Management System</h4>

<p>Our sample application is a simple patient management system for a Veterinarian that deals with Owners, Pets and Visitations. Let&#8217;s take a look at some of the models.</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;">&nbsp;
    <span style="color:#9966CC; font-weight:bold;">class</span> Owner <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
&nbsp;
      has_many <span style="color:#ff3333; font-weight:bold;">:pets</span>
      validates_presence_of <span style="color:#ff3333; font-weight:bold;">:name</span>
      validates_presence_of <span style="color:#ff3333; font-weight:bold;">:email</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">class</span> Pet <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
      belongs_to <span style="color:#ff3333; font-weight:bold;">:owner</span>
&nbsp;
      validates_presence_of  <span style="color:#ff3333; font-weight:bold;">:name</span>
      validates_presence_of  <span style="color:#ff3333; font-weight:bold;">:species</span>
      validates_inclusion_of <span style="color:#ff3333; font-weight:bold;">:species</span>, :<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span> dog cat bird snake <span style="color:#006600; font-weight:bold;">&#41;</span>,
      <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Species: %s is not included in the list of accepted species&quot;</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">class</span> Visitation <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
&nbsp;
      has_one <span style="color:#ff3333; font-weight:bold;">:pet</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>




<p>The first thing for which we&#8217;ll be adding a Feature is the process for creating a new Owner record. When a new client calls the Veterinarian&#8217;s office, an employee needs to enter them into the system.</p>

<h4>Writing a Feature</h4>

<p>Cucumber features are very straight forward. The feature file explains a set of functionality by describing different cases through scenarios.</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="" style="font-family:monospace;">&nbsp;
    Feature: Manage Owners
      In order to value
      As a role
      I want feature
&nbsp;
      Scenario: title
        Given context
        When event
        Then outcome</pre></td></tr></table></div>




<p>Each Cucumber feature represents a desired software feature that a certain user of the system wants in order to achieve some end goal.  The scenarios are formatted so that a context is setup, an event occurs and an outcome is evaluated.</p>

<p>Let&#8217;s examine our real <code>Manage Owners</code> feature.</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="" style="font-family:monospace;">&nbsp;
    Feature: Managing Owners
      In order to manage our client list
      As an employee
      I want to be able to CRUD owners
&nbsp;
      Scenario: Creating a new Owner
        Given I am on the homepage
        And I follow &quot;Owners&quot;
        Then I should be on the owners index page
        Given I follow &quot;New owner&quot;
        And I fill in &quot;Name&quot; with &quot;Clayton&quot;
        And I fill in &quot;E-Mail&quot; with &quot;clayton@example.org&quot;
        And I fill in &quot;Address&quot; with &quot;<span style="">100</span> Cactus Rd&quot;
        And I fill in &quot;City&quot; with &quot;Scottsdale&quot;
        And I fill in &quot;State&quot; with &quot;Arizona&quot;
        And I fill in &quot;Postal Code&quot; with &quot;<span style="">85000</span>&quot;
        And I fill in &quot;Phone&quot; with &quot;<span style="">480</span>-<span style="">555</span>-<span style="">1212</span>&quot;
        When I press &quot;Create&quot;
        Then I should see &quot;Owner was successfully created.&quot;
        And I should be on the owners index page</pre></td></tr></table></div>




<p class="note">Note: When you specify &#8220;And&#8221; in a Cucumber feature the step inherits the Given/Then/When from the previous step</p>

<p>Our scenario covers the act of creating a new Owner record. We first setup a context (I am on the homepage). Next we click a link in the navigation (I follow &#8220;Owners&#8221;). Once we&#8217;re on the <code>index</code> view for the Owners controller (I should be on) we can click another link to get to the <code>new</code> view for the Owners controller. From there we are simply instructing Webrat to fill out our form (I fill in) and click the submit button at the bottom (I press &#8220;Create&#8221;). Finally we are asking Webrat to read the resulting page and see if there is some text present (I should see).</p>

<p>Our feature is looking pretty good, but we still have a few more steps before we can get this scenario passing. Running the this feature from the command line will give us some output and more direction. Once you&#8217;re run <code>rake db:migrate</code> and <code>rake db:test:prepare</code> run the following from your application&#8217;s root directory:</p>



<pre>

  ruby cucumber features/manage_owners.feature

</pre>



<p>Looks like we&#8217;re failing, and Cucumber gave us some information about what&#8217;s wrong.</p>



<pre>

  Can't find mapping from &quot;the owners index page&quot; to a path.
  Now, go and add a mapping in patient-management/features/support/paths.rb

</pre>



<p>If we open up our <code>features/support/paths.rb</code> file we can add the correct path right below the default path for the &#8220;home page&#8221;. The paths file is really just a list of regular expressions that Cucumber uses to match named routes to words in scenarios.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="" style="font-family:monospace;">&nbsp;
  when /the home\s?page/
    '/'
  when /the owners index page/
    owners_path</pre></td></tr></table></div>




<p>With that little change we have our first passing Cucumber scenario! When running Cucumber features from the command line, Cucumber will print out a little summary message.</p>



<pre>

    1 scenario (1 passed)
    14 steps (14 passed)
    0m0.290s

</pre>



<h4>Complex Scenario Setup</h4>

<p>In our above example the setup, or Given steps, were pretty simple. However, sometimes you need more complex setups for your scenarios, like editing a record for example. Cucumber makes it easy to create objects in your scenarios using tables in <a href="http://wiki.github.com/aslakhellesoy/cucumber/multiline-step-arguments">multiline step arguments</a>.</p>

<p>Here is a feature that goes through the process of editing an owner&#8217;s address. You&#8217;ll see the Cucumber table that is used to setup our existing owner record. The first row in the Cucumber table correspond to some of the attribute names for our Owner 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
</pre></td><td class="code"><pre class="" style="font-family:monospace;">    Scenario: Editing an existing Owner
      Given the following owners:
       | name    | email               | address       |
       | Clayton | clayton@example.org | <span style="">100</span> Cactus Rd |
      Given I am on the homepage
      And I follow &quot;Owners&quot;
      Then I should be on the owners index page
      When I follow &quot;Edit&quot;
      And I fill in &quot;Address&quot; with &quot;<span style="">567</span> N Scottsdale Rd&quot;
      When I press &quot;Update&quot;
      Then I should see &quot;Owner was successfully updated.&quot;
      And I should be on the owners index page</pre></td></tr></table></div>




<p>When we try to run this, Cucumber will give us the &#8220;shell&#8221; for our custom step definition &#8220;Given the following owners&#8221;.</p>



<pre>
    Given /^the following owners:$/ do |table|
      # table is a Cucumber::Ast::Table
      pending # express the regexp above with the code you wish you had
    end
</pre>



<p>Since we never created a step definition for this Cucumber feature, we can go ahead and add a new file, <code>features/step_definitions/manage_owners_steps.rb</code>. It is important to note that Cucumber will load all of the files in <code>features/step_definitions</code> and that steps can be used across scenarios and features. If you think you&#8217;re going to re-use a step definition, it might be a good idea to place it in something like <code>features/step_definitions/shared_steps.rb</code>.</p>

<p class="note">Note: You should put some thought into your <a href="http://wiki.github.com/aslakhellesoy/cucumber/step-organisation">step organization</a> as your project grows in size.</p>

<p>Cucumber takes the table argument from your scenario and turns it into an array of hashes, which is technically a <code>Cucumber::Ast::Table</code> object. By iterating through each hash of the array, we can build up an object that we can use later in our tests. Since we want this object to live in the database and we don&#8217;t want to describe every attribute of the owner in our scenario, we can use Factory Girl to simplify the process.</p>

<p>The factory for our owner is simple. I won&#8217;t go into the specifics of how to create factories as that&#8217;s a whole other article. You can read more about using factories on the <a href="http://github.com/thoughtbot/factory_girl">github project page</a></p>

<p>Create a file to store your factories in <code>features/support/factories.rb</code> and copy the following.</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:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'factory_girl'</span>
&nbsp;
    Factory.<span style="color:#9900CC;">define</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:owner</span><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>o<span style="color:#006600; font-weight:bold;">|</span>
      o.<span style="color:#9900CC;">name</span> <span style="color:#996600;">&quot;John Doe&quot;</span>
      o.<span style="color:#9900CC;">email</span> <span style="color:#996600;">&quot;john@example.org&quot;</span>
      o.<span style="color:#9900CC;">address</span> <span style="color:#996600;">&quot;123 Elm Street&quot;</span>
      o.<span style="color:#9900CC;">city</span> <span style="color:#996600;">&quot;Phoenix&quot;</span>
      o.<span style="color:#9900CC;">state</span> <span style="color:#996600;">&quot;Arizona&quot;</span>
      o.<span style="color:#9900CC;">zip</span> <span style="color:#996600;">&quot;85000&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>




<p>When we use the factory to create our object, Factory Girl will take the attribute hash that we&#8217;ve passed in when creating the object and use those values for the object. Factory Girl will also fill in any blank attributes with the defaults we&#8217;ve specified in the factory definition. The record that is created will actually exist in the database, it is not a mock and it does not have any stubs, as far as our Rails app is concerned it is just a regular record in the database.</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;">    Given <span style="color:#006600; font-weight:bold;">/</span>^the following owners:$<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:#008000; font-style:italic;"># {:name =&gt; &quot;Clayton&quot;, :email =&gt; &quot;clayton@example.org&quot;, \</span>
        <span style="color:#ff3333; font-weight:bold;">:address</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;100 Cactus Rd&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
        Factory.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:owner</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>Another place we could use our <code>Given the following owners</code> step would be when creating a feature that deals with creating Pet records. An owner <code>has_many</code> pets while a pet <code>belongs_to</code> an owner. When we create a pet record, we need an existing owner record to which we&#8217;ll link the pet record. Start by creating a new feature file, <code>features/manage_pets.feature</code>.</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
</pre></td><td class="code"><pre class="" style="font-family:monospace;">    Scenario: Creating a pet
      Given the following owners:
       | name    | email               |
       | Clayton | clayton@example.org |
      Given I am on the homepage
      And I follow &quot;Pets&quot;
      And I follow &quot;New pet&quot;
      When I fill in &quot;Name&quot; with &quot;Bruno&quot;
      And I select &quot;Dog&quot; from &quot;Species&quot;
      And I select &quot;March 19th, <span style="">2005</span>&quot; as the &quot;DOB&quot; date
      And I select &quot;Clayton <span class="br0">&#40;</span>clayton@example.org<span class="br0">&#41;</span>&quot; from &quot;Owner&quot;
      When I press &quot;Create&quot;
      Then I should see &quot;Bruno was successfully created.&quot;
      And I should be on the pets index page
      And a pet named &quot;Bruno&quot; should be owned by an owner named &quot;Clayton&quot;</pre></td></tr></table></div>




<p>In this example we have re-used our <code>Given the following owners</code> table. We have also made use of some new Webrat steps like <code>I select</code> for interacting with select lists and <code>I select [DATE] as the [DATE LABEL] date</code> for selecting a date from Rails&#8217; <code>date_selector</code> helper. Finally, there is a custom step that we are going to use to make sure that the connection between our newly created pet and the existing owner is in place.</p>

<p>Let&#8217;s take a look at the step definition for our custom step above.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
    <span style="color:#9966CC; font-weight:bold;">Then</span> <span style="color:#006600; font-weight:bold;">/</span>^a pet named <span style="color:#996600;">&quot;([^<span style="color:#000099;">\&quot;</span>]*)&quot;</span> should be owned by an owner named <span style="color:#996600;">&quot;([^<span style="color:#000099;">\&quot;</span>]*)&quot;</span>$<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>pet_name, owner_name<span style="color:#006600; font-weight:bold;">|</span>
      Pet.<span style="color:#9900CC;">find_by_name</span><span style="color:#006600; font-weight:bold;">&#40;</span>pet_name<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">owner</span>.<span style="color:#9900CC;">name</span>.<span style="color:#9900CC;">should</span> == owner_name
    <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>




<p>When you place something in double quotes in your scenario steps, like &#8220;Bruno&#8221; and &#8220;Clayton&#8221; in our example, they are captured using regular expressions in the step definition. Cucumber then passes the matched values along so that then can be used in your assertion. We can find the pet based on the <code>pet_name</code> and make sure that the <code>owner</code> linked to this pet via the <code>belongs_to</code> association is the same as the <code>owner_name</code> we specified in our scenario. This is an example of how Cucumber can be used, with RSpec matchers, to make an assertion that has nothing to do with inspecting the <span class="caps">DOM </span>of a resulting webpage.</p>

<p class="note">Note: Beware of the <a href="http://wiki.github.com/aslakhellesoy/cucumber/conjunction-steps-antipattern">Conjunction Steps</a> Anti-pattern</p>

<h4>Scenario Outlines</h4>

<p>Cucumber provides a very simple way to test multiple different situations with a single scenario. These might be edge cases or just repetitive examples that don&#8217;t require their own scenario.</p>

<p>We have some business logic in our application that determines when an appointment, or <code>Visitation</code>, can be made with the Veterinarian:</p>


<ul>
<li>Visitations less than three days from today require approval before being booked</li>
<li>No visitations can be booked more than six months in advance</li>
<li>No visitations can be booked in the past</li>
</ul>



<p>We can easily represent this using a Cucumber <a href="http://wiki.github.com/aslakhellesoy/cucumber/scenario-outlines">Scenario Outline</a>. Start by creating a new feature <code>features/visitation_logic.feature</code>. Also, because we&#8217;re going to be working with time and date specific business logic, this is a good time to point out that we can stub methods when using Cucumber, however, this practice is frowned upon and should only be used for things like dates and connecting to external <span class="caps">API</span>s.</p>

<p>To add stubbing support add the following to your <code>features/support/custom_env.rb</code> file.</p>



<pre>
  require 'spec/stubs/cucumber'
</pre>




<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
</pre></td><td class="code"><pre class="" style="font-family:monospace;">&nbsp;
    Scenario Outline: visitations
      Given the following owners:
        | name    |
        | Clayton |
      And the following pets:
        | name  |
        | Bruno |
      Given today is &quot;&lt;today&gt;&quot;
      Given I am on the home page
      And I follow &quot;Visitations&quot;
      And I follow &quot;New visitation&quot;
      And I select &quot;Bruno <span class="br0">&#40;</span>Owned by Clayton<span class="br0">&#41;</span>&quot; from &quot;Pet&quot;
      And I select &quot;&lt;date&gt;&quot; as the &quot;Appointment Date&quot; date
      When I press &quot;Create&quot;
      Then I should see &quot;&lt;message&gt;&quot;
&nbsp;
    Examples:
      | today      | date               | message                                           |
      | <span style="">2010</span>-01-01 | January 2nd, <span style="">2010</span>  | Visitation requires short notice approval.        |
      | <span style="">2010</span>-01-01 | January 31st, <span style="">2010</span> | Visitation succesfully created.                   |
      | <span style="">2010</span>-01-01 | November 2nd, <span style="">2010</span> | Visitations cannot be booked that far in advance. |
      | <span style="">2010</span>-04-01 | January 2nd, <span style="">2010</span>  | Visitations cannot be booked in the past.         |</pre></td></tr></table></div>




<p>Cucumber will go through the <code>Scenario Outline</code> you have created and substitute the values in &lt;&gt;&#8217;s with their corresponding value in the <code>Examples</code> table at the bottom. Cucumber goes through each line in the <code>Examples</code> table and runs the scenario using your specified values.</p>


<p>First, lets implement the step for stubbing our today&#8217;s date.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="" style="font-family:monospace;">&nbsp;
    Given /^today is &quot;<span class="br0">&#40;</span><span class="br0">&#91;</span>^\&quot;<span class="br0">&#93;</span>*<span class="br0">&#41;</span>&quot;$/ do |date|
      Date.stub<span class="br0">&#40;</span>:today<span class="br0">&#41;</span>.and_return<span class="br0">&#40;</span>Date.parse<span class="br0">&#40;</span>date<span class="br0">&#41;</span><span class="br0">&#41;</span>
    end</pre></td></tr></table></div>




<p>In <code>features/step_definitions/shared_steps.rb</code> we can add the implementation for <code>Given the following pets</code>. We are cheating here because we&#8217;re not explicitly linking the Pet to the Owner in our scenario outline. This example isn&#8217;t so bad, but keep in mind that this association is not obvious when reading the scenario. This type of &#8220;behind the scenes&#8221; scenario setup is generally a bad idea as it obscures what&#8217;s really happening. Since we&#8217;ve already created the owner we can just find the first one in the database and create the pet records using the appropriate Rails&#8217; association method.</p>



<pre>

    Given /^the following pets:$/ do |table|
      table.hashes.each do |attributes|
        Owner.first.pets.create(attributes)
      end
    end

</pre>



<p>The other steps in our scenario already exist, either because we created them, or because they come for free with the built in steps. In this one scenario we were able to test four different business logic outcomes. This test could be expanded to go beyond what is shown on the resulting page, perhaps to ensure that an Approval record was created for the short-notice appointment.</p>

<h4>Advanced Cucumber Goodies</h4>

<p>While you should now have an understanding of the basics of Cucumber, there are a number of other powerful features.</p>

<p><strong>Tags</strong></p>

<p>Cucumber allows you to &#8220;tag&#8221; your scenarios and features so that they can be run, not run or have special other tasks run before and after them. Out of the box you get the <code>@wip</code> tag (Work In Progress) which isn&#8217;t run as part of the normal <code>rake cucumber:ok</code> process.</p>

<p><strong>Hooks</strong></p>

<p>Using tags, you can run <code>Before</code> and <code>After</code> tasks that run some code before or after a given feature or scenario. You can even run these hooks before or after all of your features. You might need to do some cleanup of generated files in an After hook or some <span class="caps">CPU </span>expensive operation before a single scenario that you don&#8217;t want running before all of your scenarios.</p>

<p><strong>Table Transformations</strong></p>

<p>The tables that we have been creating use the exact model attribute names for their header rows. This is convenient for the developer, but makes the scenarios harder to understand. This is especially true if you have very strange or legacy attribute names like <code>record_quote_ext_type__c</code>. Using table transformations you can use &#8220;Record Extension Type&#8221; in your scenario table and map that to <code>record_quote_ext_type__c</code> in your step definition.</p>

<p><strong>Calling Steps from Steps</strong></p>

<p>It&#8217;s possible to take a multi-step process, expressed in Cucumber steps, and call them all at once from another step. You might have a few steps that describe logging in to a system (filling out login credentials, pressing a button etc.) You can easily put them all into one step, <code>Given I am logged in</code>, and then reference that elsewhere. This helps to reduce unnecessary repetition of steps and keep your scenarios to a manageable size.</p>

<p><strong>Profiles</strong></p>

<p>Cucumber allows you to specify a profile to use when running your features. By default Cucumber will not run any <code>@wip</code> tagged scenarios and the output will contain any failing files and the line number of the failure. However, if you wanted to run the <code>@production</code> tagged stories and output a nice <span class="caps">HTML </span>report, you could setup a new profile for that in <code>config/cucumber.yml</code> and use it when running your features.</p>

<p><strong>Testing Javascript</strong></p>

<p>You can even use Cucumber to test complex javascript. Tools like <a href="http://celerity.rubyforge.org/">Celerity</a> with <a href="http://github.com/langalex/culerity">Culerity</a> or <a href="http://seleniumhq.org/">Selenium</a> give you the ability go beyond the interactions provided by Webrat. A new addition to Cucumber is <a href="http://github.com/jnicklas/capybara">Capybara</a> support which aims to unify the language used in steps so that you can use Webrat, Selenium and Celerity side-by-side.</p>]]></content:encoded>
			<wfw:commentRss>http://www.claytonlz.com/index.php/2010/03/zero-to-tested-with-cucumber-and-factory-girl/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Use Cucumber Table Transformations To Build Objects</title>
		<link>http://www.claytonlz.com/index.php/2010/01/cucumber-table-transformations/</link>
		<comments>http://www.claytonlz.com/index.php/2010/01/cucumber-table-transformations/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 17:30:02 +0000</pubDate>
		<dc:creator>Clayton</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[cucumber testing rails guide]]></category>

		<guid isPermaLink="false">http://www.claytonlz.com/?p=397</guid>
		<description><![CDATA[Using Cucumber&#8217;s Table Transformations, you can easily build complex objects in a way that&#8217;s easy to read and understand for clients and developers alike. My latest favorite feature of Cucumber are Table Transformations. I frequently use tables to build up complex objects and I&#8217;ve found that the regular old tables can be a little ugly, [...]]]></description>
			<content:encoded><![CDATA[<p></p><h3>Using Cucumber&#8217;s Table Transformations, you can easily build complex objects in a way that&#8217;s easy to read and understand for clients and developers alike.</h3>

<p>My latest favorite feature of Cucumber are <a href="http://wiki.github.com/aslakhellesoy/cucumber/step-argument-transforms">Table Transformations</a>. I frequently use tables to build up complex objects and I&#8217;ve found that the regular old tables can be a little ugly, especially when your attribute names don&#8217;t make much sense on their own. I&#8217;ve also noticed that building up associations can be a little wonky, usually requiring more steps than seem necessary.</p>

<h4>Conventional Table Usage</h4>

<p>Let&#8217;s look at an example of how we could use a table, without transformations, to build up some objects for our scenario.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="" style="font-family:monospace;">Scenario: Editing a Spirit
  Given I have a spirit with the following attributes:
    | spirit_type    | country_of_origin | age | brand        | lgcy_prod_sku | name       |
    | Scotch Whisky  | Scotland          | <span style="">12</span>  | The Balvenie | SC38181       | DoubleWood |
    | Scotch Whiskey | Scotland          | <span style="">12</span>  | The Macallan | SC38245       |            |</pre></td></tr></table></div>





<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="" style="font-family:monospace;">Given /^I have a spirit with the following attributes:$/ do |table|
  table.hashes.each do |attributes|
    Spirit.create!<span class="br0">&#40;</span>attributes<span class="br0">&#41;</span>
  end
end</pre></td></tr></table></div>




<p class="alert">Be sure to use <code>create!</code> in your tests to prevent false positives (Thanks Aslak!)</p>

<p>Now this is all fairly simple, and it looks pretty easy to implement, but I see some problems. First, what if you were actually friends with your <span class="caps">DBA </span>(bear with me) and you knew better than to have an attribute in your model like <code>country_of_origin</code> or <code>spirit_type</code>. Chances are those are going to be used by many other records and should be pulled out and made into their own Models, <code>Country</code> and <code>SpiritType</code> respectively.<sup class="footnote"><a href="#fn1">1</a></sup></p>

<p>So what does our scenario look like with those two new models?</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="" style="font-family:monospace;">Scenario: Editing a Spirit
  Given I have a country with the following attributes:
    | name     | continent |
    | Scotland | Europe    |
  And I have a spirit type with the following attributes:
    | name           |
    | Scotch Whiskey |
  And I have a spirit with the following attributes:
    | age | brand        | lgcy_prod_sku | name       |
    | <span style="">12</span>  | The Balvenie | SC38181       | DoubleWood |
    | <span style="">12</span>  | The Macallan | SC38245       |            |</pre></td></tr></table></div>




<p>It&#8217;s a little more complex, for sure, but it&#8217;s not totally unmanageable. However, the key part that&#8217;s missing is how to link the two spirits with their spirit types and countries of origin.</p>

<p>You could add some more steps, but then you&#8217;ve got a <a href="http://wiki.github.com/aslakhellesoy/cucumber/conjunction-steps-antipattern">conjunction step</a> which is inflexible and brittle.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="" style="font-family:monospace;">  And the spirit named &quot;The Balvenie&quot; is from &quot;Scotland&quot; and is a &quot;Scotch Whiskey&quot;</pre></td></tr></table></div>




<p>You could go back to your original step, and try to do some behind-the-scenes stuff to map <code>country_of_origin</code> to the correct <code>country_id</code>, but that gets messy too.</p>

<h4>Transform Your Tables</h4>

<p>The first step to making good use of table transformations is to make your tables more readable. Start by change the header row of your table to use meaningful representations of the real attribute names.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="" style="font-family:monospace;">Given I have a spirit with the following attributes:
  | Spirit Type    | Country  | Age | Brand        | Legacy Product Code | Name       |
  | Scotch Whisky  | Scotland | <span style="">12</span>  | The Balvenie | SC38181             | DoubleWood |
  | Scotch Whiskey | Scotland | <span style="">12</span>  | The Macallan | SC38245             |            |</pre></td></tr></table></div>




<p>We&#8217;ve turned that weird <code>lgcy_prod_sku</code> attribute into something that your Product Owner can make sense of and we&#8217;ve be able to add <code>Spirit Type</code> and <code>Country of Origin</code> back to the table. Now let&#8217;s look at the transformation that makes this all work.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="" style="font-family:monospace;">Transform /^table:Spirit Type,Country,Age,Brand,Legacy Product Code,Name$/ do |table|
  table.hashes.map do |hash|
    spirit_type = SpiritType.create!<span class="br0">&#40;</span><span class="br0">&#123;</span>:name =&gt; hash<span class="br0">&#91;</span>&quot;Spirit Type&quot;<span class="br0">&#93;</span><span class="br0">&#125;</span><span class="br0">&#41;</span>
    country = Country.create!<span class="br0">&#40;</span><span class="br0">&#123;</span>:name =&gt; hash<span class="br0">&#91;</span>&quot;Country&quot;<span class="br0">&#93;</span><span class="br0">&#125;</span><span class="br0">&#41;</span>
    spirit = Spirit.create!<span class="br0">&#40;</span><span class="br0">&#123;</span>:age =&gt; hash<span class="br0">&#91;</span>&quot;Age&quot;<span class="br0">&#93;</span>,
                            :brand =&gt; hash<span class="br0">&#91;</span>&quot;Brand&quot;<span class="br0">&#93;</span>,
                            :lgcy_prod_sku =&gt; hash<span class="br0">&#91;</span>&quot;Legacy Product Code&quot;<span class="br0">&#93;</span>,
                            :name =&gt; hash<span class="br0">&#91;</span>&quot;Name&quot;<span class="br0">&#93;</span><span class="br0">&#125;</span><span class="br0">&#41;</span>
&nbsp;
    <span class="br0">&#123;</span>:spirit_type =&gt; spirit_type, :country =&gt; country, :spirit =&gt; spirit<span class="br0">&#125;</span>
  end
end</pre></td></tr></table></div>




<p>The transformation step definition looks a lot like a regular table step definition. There is a regular expression, like anything else in Cucumber, that has the same values as the header row in the table from our scenario. Just like the table step definition we have a table object which is just an array of hashes. We can go through each hash, do the actual transformation, and then return something to our table step definition. We are using <code>map</code> (same as <code>collect</code>) to return an array of hashes, which is just what the table step definition is expecting.</p>

<p>You will also see that we&#8217;re creating three different records, which we are returning in the hash we create at the end. Let&#8217;s go through those step-by-step:</p>


<ol>
<li>Create a spirit type object from the <code>hash[&quot;Spirit Type&quot;]</code> value</li>
<li>Create a country object from the <code>hash[&quot;Country&quot;]</code> value</li>
<li>Create a spirit object from the several related hash values</li>
<li>Put all of our created objects into a hash</li>
</ol>



<p>While we&#8217;ve created the objects, we still need to create the associations. I like to leave this for the table step definition rather than the transformation since I think it&#8217;s more obvious what&#8217;s going on with the values when you&#8217;re viewing the table step.</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="" style="font-family:monospace;">Given /^I have a spirit with the following attributes:$/ do |table|
  table.each do |group|
    spirit = group<span class="br0">&#91;</span>:sprit<span class="br0">&#93;</span>
    associations = <span class="br0">&#123;</span>:country =&gt; group<span class="br0">&#91;</span>:country<span class="br0">&#93;</span>, :spirit_type =&gt; group<span class="br0">&#91;</span>:spirit_type<span class="br0">&#93;</span><span class="br0">&#125;</span>
    spirit.update_attributes<span class="br0">&#40;</span>associations<span class="br0">&#41;</span>
  end
end</pre></td></tr></table></div>




<p>There are at least a dozen ways to get the spirit associated with the country and spirit type, so don&#8217;t feel like you have to follow this pattern every time. Since we&#8217;ve sent our table an array of hashes we can iterate over each hash, <code>group</code>, and work with the individual rows. Here&#8217;s how:</p>


<ol>
<li>Extract the spirit object from the hash</li>
<li>Create another hash with the country and spirit type that Rails can make sense of</li>
<li>Use <code>update_attributes</code> to update the spirit object with the new associations</li>
</ol>



<h4>Transformation Tradeoffs</h4>

<p>We&#8217;ve been able to take our original multi-step scenario and simplify it to a single step. We are using the proper place, the step definitions, to do the associations and we have made our scenario much easier to read for non-developers working on the project. But what did we give up?</p>

<p>The biggest issue I&#8217;ve found with using table transformations is that they can be inflexible when you need to add more attributes to your dynamically created object. If you are writing features, using your table to setup objects and then realize that you need to add another attribute, you&#8217;re going to have to edit your table transformation step and how you create objects from the hash. When you take this a step further and try to have two different table definitions, you&#8217;ll be looking at having two nearly identical table transformations.<sup class="footnote"><a href="#fn2">2</a></sup></p>

<p>If you&#8217;re not already using regular old Cucumber tables to create objects, use this guide to get started. If you are using Cucumber tables to create objects, try to re-factor one of your scenarios and use the table transformation strategy. Once you start using Cucumber tables and table transformations you&#8217;ll instantly improve the readability, portability and efficiency of your steps.</p>


<p class="footnote" id="fn1"><sup>1</sup> Ignore for now the issues with <code>spirit_type</code> and Rails Single Table Inheritance</p>

<p class="footnote" id="fn2"><sup>2</sup> I&#8217;m guessing that there is some way you can get around this with regular expressions and to have more flexible transformation table steps, but I haven&#8217;t tried it yet.</p>]]></content:encoded>
			<wfw:commentRss>http://www.claytonlz.com/index.php/2010/01/cucumber-table-transformations/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>The Secret to Awesome Agile Development</title>
		<link>http://www.claytonlz.com/index.php/2009/12/awesome-agile-development-secret/</link>
		<comments>http://www.claytonlz.com/index.php/2009/12/awesome-agile-development-secret/#comments</comments>
		<pubDate>Mon, 28 Dec 2009 17:30:40 +0000</pubDate>
		<dc:creator>Clayton</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ideas]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[agile]]></category>

		<guid isPermaLink="false">http://www.claytonlz.com/?p=367</guid>
		<description><![CDATA[With a little hard work and my secret development ingredient, you can be a better Agile Developer Recently my fellow developers at Integrum and I took a survey that helped us assess our team with regard to our Agile practices. When taking the survey, and now reviewing it later on, I was struck by how [...]]]></description>
			<content:encoded><![CDATA[<p></p><h3>With a little hard work and my secret development ingredient, you can be a better Agile Developer</h3>

<p><span class="drop_cap">R</span>ecently my fellow developers at <a href="http://www.integrumtech.com">Integrum</a> and I took a survey that helped us assess our team with regard to our Agile practices. When taking the survey, and now reviewing it later on, I was struck by how many of the questions were related to a single concept. Many of the problem areas that can be uncovered by the survey, along with examples of one&#8217;s successes, come back to this one theme.</p>

<blockquote class="left">Are programmers nearly always confident that the code they&#8217;ve written recently does what it&#8217;s intended to do?
</blockquote>Consider the following questions:


<ul>
<li>Is there more than one bug per month in the business logic of completed stories?</li>
<li>Can any programmer on the team currently build and test the software, and get unambiguous success / fail result, using a single command?</li>
<li>When a programmer gets the latest code, is he nearly always confident that it will build successfully and pass all tests?</li>
<li>Are fewer than five bugs per month discovered in the team&Ecirc;&frac14;s finished work?</li>
<li>After a line item is marked &#8220;complete&#8221; do team members later perform unexpected additional work, such as bug fixes or release polish, to finish it?</li>
<li>Are programmers nearly always confident that the code they&#8217;ve written recently does what it&#8217;s intended to do?</li>
<li>Are all programmers comfortable making changes to the code?</li>
<li>Do programmers have more than one debug session per week that exceeds 10 minutes?</li>
<li>Do unexpected design changes require difficult or costly changes to existing code?</li>
<li>Do any programmers optimize code without conducting performance tests first?</li>
<li>Is there more than one bug per month in the business logic of completed stories?</li>
<li>Are any team members unsure about the quality of the software the team is producing?</li>
</ul>



<p class="alert">What&#8217;s the common theme among these stories, and the secret to better agile development? <strong>Testing</strong>, <strong>testing</strong> and more <strong>testing</strong>.</p>

<p>The negative outcomes implied by some of these questions can be solved by testing. Spending time fixing &#8220;completed&#8221; stories? Probably something you could have tested. Conversely, the positive benefits implied by other questions can be had via testing. Want to make your code more inviting and easier to deal with for new team members or people unfamiliar with the project? Give them robust and well-written tests.</p>]]></content:encoded>
			<wfw:commentRss>http://www.claytonlz.com/index.php/2009/12/awesome-agile-development-secret/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Secret to Full Stack Testing with Cucumber and Webrat</title>
		<link>http://www.claytonlz.com/index.php/2009/11/the-secret-to-full-stack-testing-with-cucumber-and-webrat/</link>
		<comments>http://www.claytonlz.com/index.php/2009/11/the-secret-to-full-stack-testing-with-cucumber-and-webrat/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 00:54:48 +0000</pubDate>
		<dc:creator>Clayton</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[cucumber]]></category>

		<guid isPermaLink="false">http://www.claytonlz.com/?p=318</guid>
		<description><![CDATA[Today I gave a presentation at "Desert Code Camp":http://www.desertcodecamp.com/ about BDD, Cucumber, Webrat and User Stories. If you'd like to find review the slides or download the code I used during the presentation here they are.]]></description>
			<content:encoded><![CDATA[<p></p><p>Today I gave a presentation at <a href="http://www.desertcodecamp.com/">Desert Code Camp</a> about <span class="caps">BDD,</span> Cucumber, Webrat and User Stories. If you&#8217;d like to find review the slides or download the code I used during the presentation here they are.</p>

<h3>Slides and Code</h3>

<p><a href="http://www.claytonlz.com/wp-content/uploads/2009/11/slides.pdf">Desert Code Camp <span class="caps">BDD</span> Presentation Slides</a></p>

<p><a href="http://github.com/clayton/desert-code-camp" target="_self">http://github.com/clayton/desert-code-camp</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.claytonlz.com/index.php/2009/11/the-secret-to-full-stack-testing-with-cucumber-and-webrat/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cucumber Table Transformations with Factory Girl Sequences</title>
		<link>http://www.claytonlz.com/index.php/2009/11/cucumber-table-transformations-with-factory-girl-sequences/</link>
		<comments>http://www.claytonlz.com/index.php/2009/11/cucumber-table-transformations-with-factory-girl-sequences/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 17:33:49 +0000</pubDate>
		<dc:creator>Clayton</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[authlogic]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[factory_girl]]></category>
		<category><![CDATA[transformations]]></category>

		<guid isPermaLink="false">http://www.claytonlz.com/?p=313</guid>
		<description><![CDATA[If you're using "Cucumber":http://cukes.info and you're not using "Transformations":http://wiki.github.com/aslakhellesoy/cucumber/step-argument-transforms you're doing it wrong. I just started using these recently and ran into a problem with creating records using "Factory Girl":http://github.com/thoughtbot/factory_girl factories that made use of sequences. While trying to create multiple "Authlogic":http://github.com/binarylogic/authlogic user records with a unique <tt>email</tt> and unique <tt>single_access_token</tt> using a Cucumber table, the functionality of <tt>Factory.next(:email)</tt> wasn't working correctly, I would always get the same e-mail address. Turns out it was an easy fix, just had to use lazy attributes in my factory.]]></description>
			<content:encoded><![CDATA[<p></p><p>If you&#8217;re using <a href="http://cukes.info">Cucumber</a> and you&#8217;re not using <a href="http://wiki.github.com/aslakhellesoy/cucumber/step-argument-transforms">Transformations</a> you&#8217;re doing it wrong. I just started using these recently and ran into a problem with creating records using <a href="http://github.com/thoughtbot/factory_girl">factory_girl</a> factories that made use of sequences. While trying to create multiple <a href="http://github.com/binarylogic/authlogic">Authlogic</a> user records with a unique <tt>email</tt> and unique <tt>single_access_token</tt> using a Cucumber table, the functionality of <tt>Factory.next(:email)</tt> wasn&#8217;t working correctly, I would always get the same e-mail address. Turns out it was an easy fix, just had to use lazy attributes in my factory.</p>

<h4>The Scenario, Step Definition and Factory</h4>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="" style="font-family:monospace;">  Scenario: Presenter List
    Given the following presenters:
      | Name    | Bio                 | Website              |
      | Clayton | Rails dev @integrum | http://claytonlz.com |
      | Chris   | Scrum @integrum     |                      |
    And I am on the homepage
    When I follow &quot;Presenters&quot;
    Then I should see &quot;Clayton&quot;
    And I should see &quot;Rails dev @integrum&quot;
    And I should see &quot;http://claytonlz.com&quot;
    Then I should see &quot;Chris&quot;
    And I should see &quot;Scrum @integrum&quot;</pre></td></tr></table></div>




<p><strong>My  Step Definition</strong><br />
This uses the Transformation table below:</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;">Given <span style="color:#006600; font-weight:bold;">/</span>^the following presenters:$<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;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>attrs<span style="color:#006600; font-weight:bold;">|</span>
    Factory.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:user</span>, attrs<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>This transformation takes a table like the one in my scenario above, and assigns the values to a hash using the actual model attribute names (Name isn&#8217;t an attribute on a user but name is). The regular cucumber step definition &#8220;consumes&#8221; this hash for each entry in the table and passes it to a Factory for creation.</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;">Transform <span style="color:#006600; font-weight:bold;">/</span>^table:Name,Bio,Website$<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;">map</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>hash<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#006600; font-weight:bold;">&#123;</span>:name <span style="color:#006600; font-weight:bold;">=&gt;</span> hash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:Name</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:bio</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> hash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:Bio</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:website</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> hash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:Website</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</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><strong>My Factory</strong></p>

<p>This is a pretty basic factory for an authlogic user model, I&#8217;m using factory_girl sequences to give me a &#8220;unique&#8221; e-mail and single access token, which are required by Authlogic.</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;">Factory.<span style="color:#9900CC;">define</span> <span style="color:#ff3333; font-weight:bold;">:user</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>user<span style="color:#006600; font-weight:bold;">|</span>
  user.<span style="color:#9900CC;">email</span> Factory.<span style="color:#9966CC; font-weight:bold;">next</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:email</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  user.<span style="color:#9900CC;">name</span> <span style="color:#996600;">&quot;&quot;</span>
  user.<span style="color:#9900CC;">bio</span> <span style="color:#996600;">&quot;&quot;</span>
  user.<span style="color:#9900CC;">website</span> <span style="color:#996600;">&quot;&quot;</span>
  user.<span style="color:#9900CC;">password</span> <span style="color:#996600;">&quot;password&quot;</span>
  user.<span style="color:#9900CC;">password_confirmation</span> <span style="color:#996600;">&quot;password&quot;</span>
  user.<span style="color:#9900CC;">single_access_token</span> Factory.<span style="color:#9966CC; font-weight:bold;">next</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:single_access_token</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>




<h4>The problem</h4>

<p>The above scenario will fail when it tries to create the user records via the factories. You&#8217;ll see a validation error about how the user model requires a unique e-mail and single access token. You&#8217;ll be wondering, &#8220;hey why are my sequences working?&#8221;. When you inspect the log you&#8217;ll see that they are in fact <span class="caps">NOT </span>working.</p>

<h4>The Quick Answer</h4>

<p>The easy answer to this is that you need to use lazy attributes in your factory for the sequences so that they are loaded each time instead of once.</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;">Factory.<span style="color:#9900CC;">define</span> <span style="color:#ff3333; font-weight:bold;">:user</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>user<span style="color:#006600; font-weight:bold;">|</span>
  user.<span style="color:#9900CC;">email</span> <span style="color:#006600; font-weight:bold;">&#123;</span> Factory.<span style="color:#9966CC; font-weight:bold;">next</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:email</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  user.<span style="color:#9900CC;">name</span> <span style="color:#996600;">&quot;&quot;</span>
  user.<span style="color:#9900CC;">bio</span> <span style="color:#996600;">&quot;&quot;</span>
  user.<span style="color:#9900CC;">website</span> <span style="color:#996600;">&quot;&quot;</span>
  user.<span style="color:#9900CC;">password</span> <span style="color:#996600;">&quot;password&quot;</span>
  user.<span style="color:#9900CC;">password_confirmation</span> <span style="color:#996600;">&quot;password&quot;</span>
  user.<span style="color:#9900CC;">single_access_token</span> <span style="color:#006600; font-weight:bold;">&#123;</span> Factory.<span style="color:#9966CC; font-weight:bold;">next</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:single_access_token</span><span style="color:#006600; font-weight:bold;">&#41;</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><strong>Notice the curly braces around the sequences</strong></p>

<h4>The Longer Answer</h4>

<p>The cucumber rdoc explains the Transform functionality, albeit somewhat hard to understand.</p>

<blockquote><p>Registers a proc that will be called with a step definition argument if it matches the pattern passed as the first argument to Transform. Alternatively, if the pattern contains captures then they will be yielded as arguments to the provided proc. The return value of the proc is consequently yielded to the step definition.</p></blockquote>

<p>I think the issue comes from something with the way these Procs are created, called and also their scope with regard to the step definition etc. I don&#8217;t think my ruby-fu is strong enough to give a good explanation but maybe I&#8217;m going in the right direction.</p>]]></content:encoded>
			<wfw:commentRss>http://www.claytonlz.com/index.php/2009/11/cucumber-table-transformations-with-factory-girl-sequences/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How To: Setup RSpec, Cucumber, Webrat, RCov and Autotest on Leopard</title>
		<link>http://www.claytonlz.com/index.php/2009/04/how-to-setup-rspec-cucumber-webrat-rcov-and-autotest-on-leopard/</link>
		<comments>http://www.claytonlz.com/index.php/2009/04/how-to-setup-rspec-cucumber-webrat-rcov-and-autotest-on-leopard/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 20:00:24 +0000</pubDate>
		<dc:creator>Clayton</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[factory girl]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[rcov]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[webrat]]></category>

		<guid isPermaLink="false">http://www.claytonlz.com/?p=220</guid>
		<description><![CDATA[RSpec, Cucumber, Webrat, RCov and Autotest are a powerful combination of tools for testing your Rails app. Unfortunately getting them to all work nicely together can be a bit of challenge. I recently configured a development environment from scratch on OS X 10.5 Leopard and kept track of all of the little details.]]></description>
			<content:encoded><![CDATA[<p></p><p>RSpec, Cucumber, Webrat, RCov and Autotest are a powerful combination of tools for testing your Rails app. Unfortunately getting them to all work nicely together can be a bit of challenge. I recently configured a development environment from scratch on OS X 10.5 Leopard and kept track of all of the little details.</p>

<h3>Prerequisites</h3>

<p>I&#8217;m assuming you&#8217;ve got the following installed:</p>


<ul>
<li>ruby</li>
<li>ruby gems 1.3.1</li>
<li><a href="http://developer.apple.com/Tools/">Apple development tools</a></li>
<li>git</li>
<li>rails &gt;= 2.3.2</li>
<li>You&#8217;ve added github to your gem sources (gem sources -a http://gems.github.com)<br />
<br /></li>
</ul>



<h3>RSpec &amp; RSpec-Rails</h3>

<p>First let&#8217;s grab the rspec<sup class="footnote"><a href="#fn1">1</a></sup> and rspec-rails<sup class="footnote"><a href="#fn2">2</a></sup> gems.</p>


<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">sudo gem install rspec</pre></div></div>





<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">sudo gem install rspec-rails</pre></div></div>




<h3>Cucumber</h3>

<p>Next we&#8217;ll install the cucumber<sup class="footnote"><a href="#fn3">3</a></sup> gem</p>


<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">sudo gem install cucumber</pre></div></div>




<h3>Webrat</h3>

<p>Webrat<sup class="footnote"><a href="#fn4">4</a></sup> is used by cucumber to simulate a browser for your integration tests. Webrat will also install nokogiri<sup class="footnote"><a href="#fn5">5</a></sup>.</p>


<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">sudo gem install webrat</pre></div></div>




<h3>RCov</h3>

<p>I thought RCov<sup class="footnote"><a href="#fn6">6</a></sup> would get installed with RSpec, but it wasn&#8217;t for me. You might not need to do this, but just to make sure&#8230;</p>


<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">sudo gem install rcov</pre></div></div>




<h3>Autotest</h3>

<p>Autotest<sup class="footnote"><a href="#fn7">7</a></sup> comes from ZenTest<sup class="footnote"><a href="#fn8">8</a></sup> and allows you to have a kick ass workflow where you are constantly running relevant tests and less-constantly automatically running your entire test suite.</p>


<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">sudo gem install ZenTest</pre></div></div>




<h3>Optionally, Thoughtbot&#8217;s Factory Girl</h3>

<p>Factory girl<sup class="footnote"><a href="#fn9">9</a></sup> is a really helpful fixture replacement (and more) gem to use in conjunction with cucumber, checkout their <a href="http://giantrobots.thoughtbot.com/2008/6/6/waiting-for-a-factory-girl">much better explanation</a></p>


<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">sudo gem install thoughtbot-factory_girl --source http://gems.github.com</pre></div></div>




<h3>Optionally, Carlos Brando&#8217;s Autotest Notification</h3>

<p>While autotest normally runs in a terminal window, it can be setup to hook into applications like <a href="http://growl.info/">growl</a> or <a href="http://www.fullphat.net/index.php">snarl</a>. The Autotest Notification<sup class="footnote"><a href="#fn9">9</a></sup> gem helps make this setup a lot easier. </p>

<p><strong>You will need growl installed and configured for this step</strong> the installation instructions on this gems github page are very easy to follow.</p>


<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">sudo gem install carlosbrando-autotest-notification --source=http://gems.github.com</pre></div></div>




<p>Next you need to turn autotest notifications &#8220;on&#8221;</p>


<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">an-install</pre></div></div>




<h3>A Sample Rails App</h3>

<p>Let&#8217;s create a sample rails app for the rest of this guide.</p>


<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">rails sample-app</pre></div></div>




<p><strong>Configuring Environment Variables</strong></p>

<p>Autotest relies on some environment variables to run all of your features and specs correctly. If autotest &#8220;hangs&#8221; after you try to run it, or it just never seems to be watching your specs or features, this will most likely solve your problem.</p>

<p>Open the test.rb environment definition file in <code>sample-app/config/environments/test.rb</code> and add the following.</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;">ENV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'AUTOFEATURE'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;true&quot;</span>
ENV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'RSPEC'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;true&quot;</span></pre></td></tr></table></div>




<p>These lines will test autotest to run, and look for changes to, your specs (rather than test unit tests) and your cucumber features.</p>

<p><strong>Update</strong></p>

<p>If you don&#8217;t want to add these environment variables to every rails project you&#8217;ve got on your machine, you can also choose to set them as environment variables in your .bash_profile or .bashrc (or whatever shell you&#8217;re using) files.</p>


<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">export AUTOFEATURE=true
export RSPEC=true</pre></div></div>




<p><strong>Unpacking Gems</strong></p>

<p>Next let&#8217;s freeze (unpack) some gems that we&#8217;ll be using in our app. I&#8217;ve run into problems trying to use the system gems with cucumber, rspec and webrat, especially when I have multiple versions of any of them installed. Unpacking them into my rails app solves this problem for me.</p>


<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">mkdir sample-app/vendor/gems
cd sample-app/vendor/gems
gem unpack rails
gem unpack rspec
gem unpack rspec-rails
gem unpack cucumber</pre></div></div>




<p>Because webrat (and nokogiri) are native gems, that is, they are built locally on your machine based on its architecture, we won&#8217;t unpack those.</p>

<p><strong>config.gem support</strong><br />
The current accepted practice, when using rails 2.3, and as suggested by the rspec guy(s) is to use rails&#8217; <code>config.gem</code> functionality.</p>

<p>Open sample-app/config/environments/test.rb and add the following lines:</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;rspec&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:lib</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;">:version</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;&gt;= 1.2.0&quot;</span> 
config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">&quot;rspec-rails&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:lib</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;">:version</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;&gt;= 1.2.0&quot;</span> 
config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">&quot;cucumber&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:lib</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;">:version</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;&gt;= 0.2.3&quot;</span>
config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">&quot;thoughtbot-factory_girl&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:lib</span>    <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;factory_girl&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:source</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;http://gems.github.com&quot;</span>
config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">&quot;webrat&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:lib</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;">:version</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;&gt;= 0.4.3&quot;</span>
config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">&quot;nokogiri&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:lib</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;">:version</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;&gt;= 1.2.3&quot;</span></pre></div></div>




<p>Your version numbers may be different, but these are all current at the time of writing.</p>

<p><strong>Boot Strapping RSpec and Cucumber</strong></p>

<p>Before you can get very far with rspec or cucumber you need to run the bootstrapping scripts to give yourself the default files and directories.</p>


<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;"># From inside your rails app sample-app/
script/generate rspec
script/generate cucumber</pre></div></div>




<p><strong>Factories</strong><br />
Depending on where you&#8217;re going to use your factories the most, you might want to save your file in either <code>spec/</code> or <code>features/</code>. I chose the latter. Only complete this step if you plan to use the FactoryGirl gem.</p>


<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">touch sample-app/features/factories.rb</pre></div></div>




<h3>Getting Accurate RCov Data</h3>

<p>By default RCov is setup to only use your specs when calculating code coverage. If you&#8217;re using Cucumber <em>and</em> RSpec, you&#8217;ll obviously want to include both types of tests to calculate your project&#8217;s true code coverage.</p>

<p>I picked up this rcov rake task from my co-worker <a href="http://jay.mcgavren.com/blog/">Jay McGavren</a> it does all of the heavy lifting for you, we&#8217;ll just need to make a couple of changes.</p>

<p>Drop <a href="http://gist.github.com/89659">this file</a> into sample-app/lib/tasks/rcov.rake and use it by calling <code>rake rcov:all</code> from your terminal.</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
</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;">'cucumber/rake/task'</span> <span style="color:#008000; font-style:italic;">#I have to add this</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'spec/rake/spectask'</span>
&nbsp;
namespace <span style="color:#ff3333; font-weight:bold;">:rcov</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#6666ff; font-weight:bold;">Cucumber::Rake::Task</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:cucumber</span><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>t<span style="color:#006600; font-weight:bold;">|</span>    
    t.<span style="color:#9900CC;">rcov</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
    t.<span style="color:#9900CC;">rcov_opts</span> = <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">--</span>rails <span style="color:#006600; font-weight:bold;">--</span>exclude osx\<span style="color:#006600; font-weight:bold;">/</span>objc,gems\<span style="color:#006600; font-weight:bold;">/</span>,spec\<span style="color:#006600; font-weight:bold;">/</span>,features\<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#006600; font-weight:bold;">--</span>aggregate coverage.<span style="color:#9900CC;">data</span><span style="color:#006600; font-weight:bold;">&#125;</span>
    t.<span style="color:#9900CC;">rcov_opts</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006600; font-weight:bold;">%</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">-</span>o <span style="color:#996600;">&quot;coverage&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#6666ff; font-weight:bold;">Spec::Rake::SpecTask</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:rspec</span><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>t<span style="color:#006600; font-weight:bold;">|</span>
    t.<span style="color:#9900CC;">spec_opts</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'--options'</span>, <span style="color:#996600;">&quot;<span style="color:#000099;">\&quot;</span>#{RAILS_ROOT}/spec/spec.opts<span style="color:#000099;">\&quot;</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    t.<span style="color:#9900CC;">spec_files</span> = FileList<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'spec/**/*_spec.rb'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    t.<span style="color:#9900CC;">rcov</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
    t.<span style="color:#9900CC;">rcov_opts</span> = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#CC00FF; font-weight:bold;">IO</span>.<span style="color:#CC0066; font-weight:bold;">readlines</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{RAILS_ROOT}/spec/rcov.opts&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>l<span style="color:#006600; font-weight:bold;">|</span> l.<span style="color:#CC0066; font-weight:bold;">chomp</span>.<span style="color:#CC0066; font-weight:bold;">split</span> <span style="color:#996600;">&quot; &quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">flatten</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  desc <span style="color:#996600;">&quot;Run both specs and features to generate aggregated coverage&quot;</span>
  task <span style="color:#ff3333; font-weight:bold;">:all</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>
    rm <span style="color:#996600;">&quot;coverage.data&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exist</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;coverage.data&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#6666ff; font-weight:bold;">Rake::Task</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;rcov:cucumber&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">invoke</span>
    <span style="color:#6666ff; font-weight:bold;">Rake::Task</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;rcov:rspec&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">invoke</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>The important part here is on line 7, we want rcov to exclude our features directory. We obviously don&#8217;t need or want rcov telling us that our feature files are not &#8220;covered&#8221;. To solve this problem we&#8217;ve simply excluded the features directory from rcov&#8217;s processing.</p>

<p>We also need to slightly modify <code>sample-app/spec/rcov.opts</code> to get the full rspec + cucumber coverage data.</p>

<p>Your rcov.opts should look like this:</p>


<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">--exclude &quot;spec/*,gems/*,features/*&quot; 
--rails
--aggregate &quot;coverage.data&quot;</pre></div></div>




<p>We again want to ignore our cucumber features and we also want to tell rcov to aggregate data in a file called coverage.data. This is used in the above rake task.</p>

<h3>Write Some Specs and Features!</h3>

<p>Act like you know what you&#8217;re doing and write some models, controllers whatever. Add some specs and features too.</p>

<h3>Autotest Workflow</h3>

<p>Open a terminal and make your way to your sample rails app and fire up autotest. You might see something like the following, depending on how many specs and features you&#8217;ve got.</p>


<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">$&gt; autotest
loading autotest/cucumber_rails_rspec
opts 
...
&nbsp;
Finished in 0.06276 seconds
&nbsp;
3 examples, 0 failures
================================================================================
&nbsp;
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby /Library/Ruby/Gems/1.8/gems/cucumber-0.2.3/bin/cucumber --format progress --format rerun --out /var/folders/Aq/Aqp06i3dFnqse+tQgQA+1++++TI/-Tmp-/autotest-cucumber.75956.0 features
.................
&nbsp;
4 scenarios
17 passed steps
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby /Library/Ruby/Gems/1.8/gems/rspec-1.2.2/bin/spec --autospec spec/models/intern_spec.rb -O spec/spec.opts 
...
&nbsp;
Finished in 0.062995 seconds
&nbsp;
3 examples, 0 failures
================================================================================
&nbsp;
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby /Library/Ruby/Gems/1.8/gems/cucumber-0.2.3/bin/cucumber --format progress --format rerun --out /var/folders/Aq/Aqp06i3dFnqse+tQgQA+1++++TI/-Tmp-/autotest-cucumber.75956.1 features
.................
&nbsp;
4 scenarios
17 passed steps</pre></div></div>




<h3>The <span class="caps">REALLY </span>important stuff</h3>


<ol>
<li>make sure you&#8217;ve got &#8220;ENV['AUTOFEATURE'] = true&#8221; in your test.rb otherwise autotest won&#8217;t run your features automatically</li>
<li>make sure you&#8217;ve got &#8220;ENV['RSPEC'] = true&#8221; in your bash profile or else autotest won&#8217;t run your specs automatically</li>
<li>make sure you&#8217;ve got &#8220;&#8211;aggregate = &#8216;coverage.data&#8217;&#8221; in your spec/rcov.opts file if you&#8217;re going to use the above rake task and hope to get combined rcov coverage data between rspec and cucumber</li>
<li>make sure you&#8217;re excluding the features directory from rcov where required or else you&#8217;ll end up with misleading rcov data.</li>
</ol>



<h3>Gem Versions</h3>

<p>Here&#8217;s a list of the current gems and their versions that I used in preparing this guide.</p>


<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">*** LOCAL GEMS ***
&nbsp;
actionmailer (2.3.2, 1.3.6, 1.3.3)
actionpack (2.3.2, 1.13.6, 1.13.3)
actionwebservice (1.2.6, 1.2.3)
activerecord (2.3.2, 1.15.6, 1.15.3)
activeresource (2.3.2)
activesupport (2.3.2, 1.4.4, 1.4.2)
acts_as_ferret (0.4.1)
addressable (2.0.2)
builder (2.1.2)
capistrano (2.0.0)
carlosbrando-autotest-notification (1.9.1)
cgi_multipart_eof_fix (2.5.0, 2.2)
cucumber (0.2.3)
daemons (1.0.9, 1.0.7)
data_objects (0.9.11)
diff-lcs (1.1.2)
dnssd (0.6.0)
extlib (0.9.11)
fastthread (1.0.1, 1.0)
fcgi (0.8.7)
ferret (0.11.4)
gem_plugin (0.2.3, 0.2.2)
highline (1.2.9)
hpricot (0.6)
libxml-ruby (0.3.8.4)
mongrel (1.1.4, 1.0.1)
mysql (2.7)
needle (1.3.0)
net-sftp (1.1.0)
net-ssh (1.1.2)
nokogiri (1.2.3)
polyglot (0.2.5)
rack (0.9.1)
rails (2.3.2, 1.2.6, 1.2.3)
rake (0.8.4, 0.7.3)
rcov (0.8.1.2.0)
RedCloth (3.0.4)
rspec (1.2.2)
rspec-rails (1.2.2)
ruby-openid (1.1.4)
ruby-yadis (0.3.4)
rubynode (0.1.3)
sources (0.0.1)
sqlite3-ruby (1.2.1)
term-ansicolor (1.0.3)
termios (0.9.4)
textmate (0.9.2)
thor (0.9.9)
thoughtbot-factory_girl (1.2.0)
treetop (1.2.5)
webrat (0.4.3)
ZenTest (4.0.0)</pre></div></div>




<h3>El Fin</h3>

<p>Hopefully this guide was useful or had that one little step that you needed to get everything working. I&#8217;m sure this will all be out of date in the coming weeks, but I&#8217;ll try to keep it as up-to-date as possible. If you see any errors, or can better explain some of the missing pieces, please post a comment. Thanks!</p>

<p class="footnote" id="fn1"><sup>1</sup> <a href="http://github.com/dchelimsky/rspec/tree/master">http://github.com/dchelimsky/rspec/tree/master</a></p>

<p class="footnote" id="fn2"><sup>2</sup> <a href="http://github.com/dchelimsky/rspec-rails/tree/master">http://github.com/dchelimsky/rspec-rails/tree/master</a></p>

<p class="footnote" id="fn3"><sup>3</sup> <a href="http://github.com/aslakhellesoy/cucumber/tree/master">http://github.com/aslakhellesoy/cucumber/tree/master</a></p>

<p class="footnote" id="fn4"><sup>4</sup> <a href="http://wiki.github.com/brynary/webrat">http://wiki.github.com/brynary/webrat</a></p>

<p class="footnote" id="fn5"><sup>5</sup> <a href="http://github.com/tenderlove/nokogiri/tree/master">http://github.com/tenderlove/nokogiri/tree/master</a> </p>

<p class="footnote" id="fn6"><sup>6</sup> <a href="http://rubyforge.org/projects/rcov/">http://rubyforge.org/projects/rcov/</a></p>

<p class="footnote" id="fn7"><sup>7</sup> <a href="http://www.zenspider.com/ZSS/Products/ZenTest/#rsn">http://www.zenspider.com/ZSS/Products/ZenTest/#rsn</a></p>

<p class="footnote" id="fn8"><sup>8</sup> <a href="http://www.zenspider.com/ZSS/Products/ZenTest/">http://www.zenspider.com/ZSS/Products/ZenTest/</a></p>

<p class="footnote" id="fn9"><sup>9</sup> <a href="http://github.com/thoughtbot/factory_girl/tree/master">http://github.com/thoughtbot/factory_girl/tree/master</a></p>

<p class="footnote" id="fn10"><sup>10</sup> <a href="http://github.com/carlosbrando/autotest-notification/tree/master">http://github.com/carlosbrando/autotest-notification/tree/master</a></p>

<p><strong>Updates</strong><br />
2009-12-08 &#8211; Removed &#8220;sudo&#8221; when describing how to unpack gems (h/t <a href="http://twitter.com/xdotcommer">xdotcommer</a>)</p>]]></content:encoded>
			<wfw:commentRss>http://www.claytonlz.com/index.php/2009/04/how-to-setup-rspec-cucumber-webrat-rcov-and-autotest-on-leopard/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
		<item>
		<title>RSpec Shared Example before(:each) Gotcha</title>
		<link>http://www.claytonlz.com/index.php/2009/03/rspec-shared-example-before-each-gotcha/</link>
		<comments>http://www.claytonlz.com/index.php/2009/03/rspec-shared-example-before-each-gotcha/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 20:04:31 +0000</pubDate>
		<dc:creator>Clayton</dc:creator>
				<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[rspec]]></category>

		<guid isPermaLink="false">http://www.claytonlz.com/?p=124</guid>
		<description><![CDATA[Shared example groups are a great feature of Rspec that help you simplify your tests and keep your code DRY. You setup shared example groups almost exactly like you would a regular set of specs, but these similarities can be slightly misleading.]]></description>
			<content:encoded><![CDATA[<p></p><p>Shared example groups are a great feature of Rspec that help you simplify your tests and keep your code <span class="caps">DRY.</span> You setup shared example groups almost exactly like you would a regular set of specs, but these similarities can be slightly misleading.</p>

<p>Below we have an example model, spec and <a href="http://rspec.info/documentation/">shared example group</a>. Our Dog model has its own set of functionality, but as a mammal it should still have some aspects of being a mammal. We&#8217;ve got some specs in a shared example group that we use for testing all of our mammal models to make sure things don&#8217;t get too out of whack in the universe.</p>

<h3>Our Example Model</h3>


<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> Dog
&nbsp;
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:mammal</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">mammal</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> greet
    <span style="color:#996600;">&quot;Hi, I'm #{self.name}, woof woof!&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>




<h3>Our Example Spec</h3>


<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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">describe Dog <span style="color:#9966CC; font-weight:bold;">do</span>
&nbsp;
  before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#0066ff; font-weight:bold;">@animal</span> = Dog.<span style="color:#9900CC;">new</span>
    <span style="color:#0066ff; font-weight:bold;">@animal</span>.<span style="color:#9900CC;">name</span> = <span style="color:#996600;">&quot;Bruno&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  it_should_behave_like <span style="color:#996600;">&quot;a mammal&quot;</span>
&nbsp;
  describe <span style="color:#996600;">&quot;Greet&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    it <span style="color:#996600;">&quot;should respond with its name and a greeting&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#0066ff; font-weight:bold;">@animal</span>.<span style="color:#9900CC;">greet</span>.<span style="color:#9900CC;">should</span> == <span style="color:#996600;">&quot;Hi, I'm Bruno, woof woof!&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;">end</span></pre></td></tr></table></div>




<h3>Our Shared Spec</h3>


<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;">describe <span style="color:#996600;">&quot;a mammal&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:shared</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  it <span style="color:#996600;">&quot;should really be a mammal&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#0066ff; font-weight:bold;">@animal</span>.<span style="color:#9900CC;">mammal</span>.<span style="color:#9900CC;">should</span> be_true
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>




<h3>A Typical <code>before(:each)</code></h3>

<p>Typically, when you&#8217;ve got a describe block, you might use <code>before(:each)</code> to setup some scenario that is used for each spec in that describe block, pretty normal RSpec stuff. We&#8217;re using it above in our example spec to create a new Dog object and set that dog&#8217;s name.</p>

<h3>Using <code>before(:each)</code> in a shared spec</h3>

<p>What if you wanted to use a <code>before(:each)</code> in your shared spec? Expanding on our example above we can do something like this.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">describe <span style="color:#996600;">&quot;a mammal&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:shared</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#0066ff; font-weight:bold;">@animal</span>.<span style="color:#9900CC;">stub</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:has_body_hair</span>?<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">and_return</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  it <span style="color:#996600;">&quot;should really be a mammal&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#0066ff; font-weight:bold;">@animal</span>.<span style="color:#9900CC;">mammal</span>.<span style="color:#9900CC;">should</span> be_true
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>




<p>Based on typical RSpec behavior, one would think that the stubbing of the <code>has_by_hair?</code> method on the instance of an animal, would only apply to the specs inside of the <code>describe</code> block of the shared example group. <strong>However, by specifying in the Dog spec that a dog &#8220;should behave like&#8221; a mammal, and thus using the shared spec, that stub will apply to all subsequent &#8220;it should&#8221; blocks in your model spec.</strong></p>

<p>What if, for example, we had the following in our Dog spec.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  describe <span style="color:#996600;">&quot;Mutate into Lizard Dog&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
   <span style="color:#008000; font-style:italic;"># dog.mutate will remove body hair and make the dog cold blooded</span>
    it <span style="color:#996600;">&quot;should mutate into a new species&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#0066ff; font-weight:bold;">@animal</span>.<span style="color:#9900CC;">mutate</span>
      <span style="color:#0066ff; font-weight:bold;">@animal</span>.<span style="color:#9900CC;">has_body_hair</span>?.<span style="color:#9900CC;">should</span> be_false
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>




<p>If we include this in our Dog spec, below the inclusion of the shared example spec, our test will fail. We&#8217;ve already stubbed out the <code>has_body_hair?</code> method as part of our shared example group, when we call it down here in this completely separate describe block, RSpec is just using the stub we setup previously.</p>

<h3>It might be a design problem if&#8230;</h3>

<p>Now while I&#8217;m considering this a gotcha, it may be that this is expected behavior, I couldn&#8217;t find anything specifically when researching this &#8220;bug&#8221; originally. It is also possible that stubbing behavior in shared example groups is frowned upon, and I&#8217;m just &#8220;doing it wrong&#8221;.</p>

<p>Ultimately, I tried using patterns that made sense to me and seemed to be in line with how RSpec works in general. A stubbed method inside the <code>before(:each)</code> of a describe block is usually only applicable to the specs and nested describes contained within. When I realized that this is <em>not</em> the case with shared example groups, it seemed like a gotcha.</p>]]></content:encoded>
			<wfw:commentRss>http://www.claytonlz.com/index.php/2009/03/rspec-shared-example-before-each-gotcha/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
