<?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; Ruby On Rails</title>
	<atom:link href="http://www.claytonlz.com/index.php/category/ruby/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 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>Missing host to link to! Please provide :host parameter</title>
		<link>http://www.claytonlz.com/index.php/2009/11/authlogic-activation-host-parameter-gotcha/</link>
		<comments>http://www.claytonlz.com/index.php/2009/11/authlogic-activation-host-parameter-gotcha/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 08:53:46 +0000</pubDate>
		<dc:creator>Clayton</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[activation]]></category>
		<category><![CDATA[authlogic]]></category>
		<category><![CDATA[gotcha]]></category>

		<guid isPermaLink="false">http://www.claytonlz.com/?p=305</guid>
		<description><![CDATA[If you've followed my version of the "authlogic account activation tutorial":http://www.claytonlz.com/index.php/2009/07/authlogic-account-activation-tutorial/ or the "original version":http://github.com/matthooks/authlogic-activation-tutorial by Matt Hooks you might have run into this error]]></description>
			<content:encoded><![CDATA[<p></p><p>If you&#8217;ve followed my version of the <a href="http://www.claytonlz.com/index.php/2009/07/authlogic-account-activation-tutorial/">authlogic account activation tutorial</a> or the <a href="http://github.com/matthooks/authlogic-activation-tutorial">original version</a> by Matt Hooks you might have run into this error:</p>

<code>
Missing host to link to! Please provide :host parameter or set default_url_options[:host] when sending emails
</code>

<p>When authlogic sends e-mails with the account activation link, it uses a <tt>url_for</tt> helper to build that link. Because the &#8220;Notifier&#8221; mailer is an instance of <tt>ActionMailer::Base</tt> and not <tt>ActionController::Base</tt> it doesn&#8217;t know what the <tt>host</tt> parameter of the <span class="caps">URL </span>should be, so you have to tell it explicitly.</p>

<p>Put the following into your <tt>environments/development.rb</tt> and <tt>environments/test.rb</tt>:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># This assumes you're running your local development server on port 3000 via script/server</span>
config.<span style="color:#9900CC;">action_mailer</span>.<span style="color:#9900CC;">default_url_options</span> = <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:host</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;127.0.0.1:3000&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>




<p>Put this into your <tt>environments/production.rb</tt>:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Replace example.org with your actual domain name</span>
config.<span style="color:#9900CC;">action_mailer</span>.<span style="color:#9900CC;">default_url_options</span> = <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:host</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;example.org&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>
]]></content:encoded>
			<wfw:commentRss>http://www.claytonlz.com/index.php/2009/11/authlogic-activation-host-parameter-gotcha/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Does the Chronic Time Parsing Library Break with DST Changes?</title>
		<link>http://www.claytonlz.com/index.php/2009/11/ruby-chronic-dst-error/</link>
		<comments>http://www.claytonlz.com/index.php/2009/11/ruby-chronic-dst-error/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 17:30:15 +0000</pubDate>
		<dc:creator>Clayton</dc:creator>
				<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[chronic]]></category>
		<category><![CDATA[gems]]></category>
		<category><![CDATA[weirdness]]></category>

		<guid isPermaLink="false">http://www.claytonlz.com/?p=298</guid>
		<description><![CDATA[I've been troubleshooting a problem on an existing application for the last week or so that deals with the parsing of dates using the <a href="http://chronic.rubyforge.org/">Chronic</a> time parsing library. Today the problem magically solved itself, without me doing anything. These types of self solving problems are usually more frustrating than problems you can't solve at all, so I took a little extra time to experiment with the particular date format I was using and found what might be a problem with the Chronic library when it gets to the "fall back" DST change in the fall.]]></description>
			<content:encoded><![CDATA[<p></p><p>I&#8217;ve been troubleshooting a problem on an existing application for the last week or so that deals with the parsing of dates using the <a href="http://chronic.rubyforge.org/">Chronic</a> time parsing library. Today the problem magically solved itself, without me doing anything. These types of self solving problems are usually more frustrating than problems you can&#8217;t solve at all, so I took a little extra time to experiment with the particular date format I was using and found what might be a problem with the Chronic library when it gets to the &#8220;fall back&#8221; DST change in the fall.</p>

<h4>Do You Observe <span class="caps">DST</span>?</h4>

<p>It looks like if your local environment is set to a timezone which observes <span class="caps">DST, </span>like Eastern Standard Time for instance, you get a chunk of dates around the switch in the fall where Chronic returns <tt>nil</tt> instead of the correct date. If you&#8217;re on a machine where the timezone is set to an area which does not observe <span class="caps">DST, </span>like Arizona, you won&#8217;t be able to replicate this problem.</p>

<h4>Replicating the problem</h4>

<p>Install the latest version of Chronic.</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">sudo</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> chronic</pre></div></div>




<p>Fire up <tt>irb</tt> and give the following a shot.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">require <span style="color: #ff0000;">'chronic'</span>
<span style="color: #7a0874; font-weight: bold;">&#40;</span>Date.parse<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;2009-01-01&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>..Date.parse<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;2009-12-31&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>.each <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #000000; font-weight: bold;">|</span>d<span style="color: #000000; font-weight: bold;">|</span>
  puts Chronic.parse<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;next tuesday 6am&quot;</span>, :now =<span style="color: #000000; font-weight: bold;">&gt;</span> d<span style="color: #7a0874; font-weight: bold;">&#41;</span>
end</pre></td></tr></table></div>




<p>With <tt>ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]</tt> on Ubuntu and my timezone set to <span class="caps">EST</span>/New York. I see a bunch of Tuesdays and then a blank section around the end of October / beginning of November.</p>



<pre>
...snip...
Tue Oct 20 06:00:00 -0400 2009
Tue Oct 20 06:00:00 -0400 2009
Tue Oct 27 06:00:00 -0400 2009
Tue Oct 27 06:00:00 -0400 2009
Tue Oct 27 06:00:00 -0400 2009
Tue Oct 27 06:00:00 -0400 2009
Tue Oct 27 06:00:00 -0400 2009
Tue Oct 27 06:00:00 -0400 2009
Tue Oct 27 06:00:00 -0400 2009
nil
nil
nil
nil
nil
nil
Tue Nov 03 06:00:00 -0500 2009
Tue Nov 10 06:00:00 -0500 2009
Tue Nov 10 06:00:00 -0500 2009
Tue Nov 10 06:00:00 -0500 2009
Tue Nov 10 06:00:00 -0500 2009
Tue Nov 10 06:00:00 -0500 2009
Tue Nov 10 06:00:00 -0500 2009
Tue Nov 10 06:00:00 -0500 2009
Tue Nov 17 06:00:00 -0500 2009
Tue Nov 17 06:00:00 -0500 2009
Tue Nov 17 06:00:00 -0500 2009
...snip...
</pre>



<p>If you change your timezone to something like <span class="caps">MST</span>/Arizona you&#8217;ll see this</p>



<pre>
...snip...
Tue Oct 20 06:00:00 -0400 2009
Tue Oct 20 06:00:00 -0400 2009
Tue Oct 27 06:00:00 -0400 2009
Tue Oct 27 06:00:00 -0400 2009
Tue Oct 27 06:00:00 -0400 2009
Tue Oct 27 06:00:00 -0400 2009
Tue Oct 27 06:00:00 -0400 2009
Tue Oct 27 06:00:00 -0400 2009
Tue Oct 27 06:00:00 -0400 2009
Tue Nov 03 06:00:00 -0500 2009
Tue Nov 03 06:00:00 -0500 2009
Tue Nov 03 06:00:00 -0500 2009
Tue Nov 03 06:00:00 -0500 2009
Tue Nov 03 06:00:00 -0500 2009
Tue Nov 03 06:00:00 -0500 2009
Tue Nov 03 06:00:00 -0500 2009
Tue Nov 10 06:00:00 -0500 2009
Tue Nov 10 06:00:00 -0500 2009
Tue Nov 10 06:00:00 -0500 2009
Tue Nov 10 06:00:00 -0500 2009
Tue Nov 10 06:00:00 -0500 2009
Tue Nov 10 06:00:00 -0500 2009
Tue Nov 10 06:00:00 -0500 2009
Tue Nov 17 06:00:00 -0500 2009
Tue Nov 17 06:00:00 -0500 2009
Tue Nov 17 06:00:00 -0500 2009
...snip...
</pre>



<h4>What&#8217;s the Fix?</h4>

<p>None as far as I know. I&#8217;m sure one could dig into the internals of the library and figure out how to deal with this problem, but I&#8217;m not up for that right now.</p>]]></content:encoded>
			<wfw:commentRss>http://www.claytonlz.com/index.php/2009/11/ruby-chronic-dst-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Authlogic Account Activation Tutorial</title>
		<link>http://www.claytonlz.com/index.php/2009/07/authlogic-account-activation-tutorial/</link>
		<comments>http://www.claytonlz.com/index.php/2009/07/authlogic-account-activation-tutorial/#comments</comments>
		<pubDate>Wed, 22 Jul 2009 07:57:27 +0000</pubDate>
		<dc:creator>Clayton</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[authlogic]]></category>

		<guid isPermaLink="false">http://www.claytonlz.com/?p=274</guid>
		<description><![CDATA[I found a great tutorial explaining how to setup user activation with authlogic, but it was a little hard to read so I've dumped it all into one easier to read file.]]></description>
			<content:encoded><![CDATA[<p></p><p>I found a great tutorial explaining how to setup user activation with authlogic, but it was a little hard to read so I&#8217;ve dumped it all into one easier to read file.</p>

<p>Here&#8217;s the original: http://github.com/matthooks/authlogic-activation-tutorial/tree/master<br />
Here&#8217;s my fork on github: http://github.com/clayton/authlogic-activation-tutorial/tree/master</p>

<p>Below is the formatted guide that is on github. </p>

<p><strong>Remember, all credit goes to <a href="http://github.com/matthooks">Matt Hooks</a> and his original <a href="http://github.com/matthooks/authlogic-activation-tutorial/tree/master">Authlogic Account Activation Tutorial</a></strong></p>

<h2>Introduction</h2>

<p>This is an easier to read version of Matt Hooks&#8217; <a href="http://github.com/matthooks/authlogic-activation-tutorial/tree/master">Authlogic Activation Tutorial</a>. The tutorial is divided into a number of steps and walks through the process of implementing user activation functionality into your pre-existing Rails app using <a href="http://github.com/binarylogic/authlogic/tree/master">Authlogic</a>. If you are just starting out with Authlogic, be sure to checkout the <a href="http://github.com/binarylogic/authlogic_example/tree/master">Authlogic Example Tutorial</a>.</p>

<h2>Step 1</h2>

<p>Let&#8217;s begin by adding an &#8216;active&#8217; field with a default of false to the user model.</p>

<p><code>script/generate migration AddActiveToUsers active:boolean</code></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:#008000; font-style:italic;"># new migration XXX_add_active_to_users.rb</span>
  <span style="color:#9966CC; font-weight:bold;">class</span> AddActiveToUsers <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
      add_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:active</span>, <span style="color:#ff3333; font-weight:bold;">:boolean</span>, <span style="color:#ff3333; font-weight:bold;">:default</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;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
      remove_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:active</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>




<h2>Step 2</h2>

<p>Don&#8217;t forget to run the migraiton.</p>

<p><code>rake db:migrate</code></p>

<p>Authlogic automatically executes the following methods, if present, upon user action: active?, approved?, and confirmed?. Let&#8217;s create an &#8220;active?&#8221; method so we can hook into this magical goodness. And we should make sure that we protect the active attribute from mass-assignments by calling attr_accessible.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;"># added to user.rb</span>
  attr_accessible <span style="color:#ff3333; font-weight:bold;">:login</span>, <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:password</span>, <span style="color:#ff3333; font-weight:bold;">:password_confirmation</span>, <span style="color:#ff3333; font-weight:bold;">:openid_identifier</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> active?
    active
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>




<h2>Step 3</h2>

<p>Now try to log in. You should receive the error, &#8220;Your account is not active.&#8221; So far so good. Let&#8217;s make a controller to handle our activations:</p>

<p><code>script/generate controller activations new create</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
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;"># new file app/controllers/activations_controller.rb</span>
  <span style="color:#9966CC; font-weight:bold;">class</span> ActivationsController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
    before_filter <span style="color:#ff3333; font-weight:bold;">:require_no_user</span>, <span style="color:#ff3333; font-weight:bold;">:only</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:new</span>, <span style="color:#ff3333; font-weight:bold;">:create</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> new
      <span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">find_using_perishable_token</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:activation_code</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#006666;">1</span>.<span style="color:#9900CC;">week</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">active</span>?
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> create
      <span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
      <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">active</span>?
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">activate</span>!
        <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">deliver_activation_confirmation</span>!
        redirect_to account_url
      <span style="color:#9966CC; font-weight:bold;">else</span>
        render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:new</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>




<h2>Step 4</h2>

<p>I raise exceptions in these actions to make sure that someone who is already active cannot re-activate their account and to deal with an invalid perishable token. I&#8217;ll leave it up to you how you want to handle these errors &#8212; you should probably provide some sort of &#8220;My Token is Expired!&#8221; action that will reset the token and resend the activation email if the user does not get around to activating right away.</p>

<p>Going down the list, let&#8217;s define the missing actions. First:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;"># added to user.rb</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> activate!
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">active</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
    save
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>




<h2>Step 5</h2>

<p>Next, let&#8217;s make sure our user gets an e-mail with his activation code when he signs up. How are we getting our activation code? The same way we get our password reset code &#8212; through our perishable token:</p>

<p><strong>Update</strong>: If you are experiencing the &#8220;Missing host to link to! Please provide :host parameter &#8221; error see this post <a href="http://www.claytonlz.com/index.php/2009/11/authlogic-activation-host-parameter-gotcha/">Missing host to link to! Please provide :host parameter</a></p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">  # added to app/models/user.rb
  def deliver_activation_instructions!
    reset_perishable_token!
    Notifier.deliver_activation_instructions(self)
  end
&nbsp;
  def deliver_activation_confirmation!
    reset_perishable_token!
    Notifier.deliver_activation_confirmation(self)
  end
&nbsp;
  # added to app/models/notifier.rb
  def activation_instructions(user)
    subject       &quot;Activation Instructions&quot;
    from          &quot;Binary Logic Notifier &lt;noreply@binarylogic.com&gt;&quot;
    recipients    user.email
    sent_on       Time.now
    body          :account_activation_url =&gt; register_url(user.perishable_token)
  end
&nbsp;
  def activation_confirmation(user)
    subject       &quot;Activation Complete&quot;
    from          &quot;Binary Logic Notifier &lt;noreply@binarylogic.com&gt;&quot;
    recipients    user.email
    sent_on       Time.now
    body          :root_url =&gt; root_url
  end
&nbsp;
  # added to config/routes.rb
  map.register '/register/:activation_code', :controller =&gt; 'activations', :action =&gt; 'new'
  map.activate '/activate/:id', :controller =&gt; 'activations', :action =&gt; 'create'
&nbsp;
  &lt;!-- new file app/views/notifier/activation_instructions.erb --&gt; 
  Thank you for creating an account! Click the url below to activate your account!
&nbsp;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@account_activation_url</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
  If the above URL does not work try copying and pasting it into your browser. If you continue to have problem, please feel free to contact us.
&nbsp;
  &lt;!-- new file app/views/notifier/activation_confirmation.erb --&gt;
  Your account has been activated.
&nbsp;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@root_url</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
  If the above URL does not work try copying and pasting it into your browser. If you continue to have problem, please feel free to contact us.</pre></td></tr></table></div>




<h2>Step 6</h2>

<p>Now let&#8217;s modify the user create action:</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="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;"># modified app/controllers/users_controller.rb</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> create
    <span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:user</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># Saving without session maintenance to skip</span>
    <span style="color:#008000; font-style:italic;"># auto-login which can't happen here because</span>
    <span style="color:#008000; font-style:italic;"># the User has not yet been activated</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">save_without_session_maintenance</span>
      <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">deliver_activation_instructions</span>!
      flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Your account has been created. Please check your e-mail for your account activation instructions!&quot;</span>
      redirect_to root_url
    <span style="color:#9966CC; font-weight:bold;">else</span>
      render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:new</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>




<h2>Step 7</h2>

<p>As the comment says, we don&#8217;t need the Authlogic auto-login to take place so we save without maintaining the session. Now let&#8217;s define the &#8216;register&#8217; view.</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;">  &lt;!-- new file app/views/activations/new.html.erb --&gt;
&nbsp;
  &lt;h1&gt;Activate your account&lt;/h1&gt;
&nbsp;
  <span style="color:#006600; font-weight:bold;">&lt;%</span> form_for <span style="color:#0066ff; font-weight:bold;">@user</span>, <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> activate_path<span style="color:#006600; font-weight:bold;">&#40;</span>@user.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#ff3333; font-weight:bold;">:html</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:method</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:post</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  	<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">error_messages</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  	<span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">submit</span> <span style="color:#996600;">&quot;Activate&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></td></tr></table></div>




<h2>Step 8</h2>

<p>Let&#8217;s see if things are working&#8230;</p>

<p>&#8230; (processing) &#8230;</p>

<p>Looks like our user got activated!</p>

<p>But there&#8217;s a slight problem. Since we didn&#8217;t update the user&#8217;s password, we didn&#8217;t get a magical Authlogic auto-login! How rude.</p>

<p>At this point it&#8217;s perfectly fine to let the user log themselves in. And you can certainly simplify the activation down to one action so the user doesn&#8217;t have to click another button. But, I like Authlogic&#8217;s session maintenance. I also like short signup forms. So let&#8217;s kill two birds with one stone.</p>

<p>Let&#8217;s set up the user creation form to only ask for a user&#8217;s login/email. Then, let&#8217;s ask the user to set their password/openid upon activation, which will log them in automatically.</p>

<p>First, let&#8217;s change our acts_as_authentic call to only check for password length on update if the user has no credentials set.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;"># modified user.rb</span>
  <span style="color:#008000; font-style:italic;"># For authlogic 2.0+</span>
  acts_as_authentic <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>c<span style="color:#006600; font-weight:bold;">|</span>
    c.<span style="color:#9900CC;">validates_length_of_password_field_options</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>:on <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:update</span>, <span style="color:#ff3333; font-weight:bold;">:minimum</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">4</span>, :<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:has_no_credentials</span>?<span style="color:#006600; font-weight:bold;">&#125;</span>
    c.<span style="color:#9900CC;">validates_length_of_password_confirmation_field_options</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>:on <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:update</span>, <span style="color:#ff3333; font-weight:bold;">:minimum</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">4</span>, :<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:has_no_credentials</span>?<span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Pre-authlogic 2.0</span>
  <span style="color:#008000; font-style:italic;"># acts_as_authentic :login_field_validation_options =&gt; { :if =&gt; :openid_identifier_blank? },</span>
  <span style="color:#008000; font-style:italic;">#                   :password_field_validation_options =&gt; { :if =&gt; :openid_identifier_blank? },</span>
  <span style="color:#008000; font-style:italic;">#                   :password_field_validates_length_of_options =&gt; { :on =&gt; :update, :if =&gt; :has_no_credentials? }</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># ...</span>
  <span style="color:#008000; font-style:italic;"># we need to make sure that either a password or openid gets set</span>
  <span style="color:#008000; font-style:italic;"># when the user activates his account</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> has_no_credentials?
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">crypted_password</span>.<span style="color:#9900CC;">blank</span>? <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">openid_identifier</span>.<span style="color:#9900CC;">blank</span>?
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># ...</span>
  <span style="color:#008000; font-style:italic;"># now let's define a couple of methods in the user model. The first</span>
  <span style="color:#008000; font-style:italic;"># will take care of setting any data that you want to happen at signup</span>
  <span style="color:#008000; font-style:italic;"># (aka before activation)</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> signup!<span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">login</span> = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:user</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:login</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">email</span> = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:user</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:email</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    save_without_session_maintenance
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># the second will take care of setting any data that you want to happen</span>
  <span style="color:#008000; font-style:italic;"># at activation. at the very least this will be setting active to true</span>
  <span style="color:#008000; font-style:italic;"># and setting a pass, openid, or both.</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> activate!<span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">active</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">password</span> = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:user</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:password</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">password_confirmation</span> = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:user</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:password_confirmation</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">openid_identifier</span> = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:user</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:openid_identifier</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    save
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># modified activations_controller.rb</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> create
    <span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">active</span>?
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">activate</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">deliver_activation_confirmation</span>!
      flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Your account has been activated.&quot;</span>
      redirect_to account_url
    <span style="color:#9966CC; font-weight:bold;">else</span>
      render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:new</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># modified users_controller.rb</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> create
    <span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">new</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">signup</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">deliver_activation_instructions</span>!
      flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Your account has been created. Please check your e-mail for your account activation instructions!&quot;</span>
      redirect_to root_url
    <span style="color:#9966CC; font-weight:bold;">else</span>
      render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:new</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>




<h2>Step 10</h2>

<p>Now we need to update our views to reflect the new signup process.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">  &lt;!-- modified app/views/activations/new.html.erb --&gt;
  &lt;h1&gt;Activate your account&lt;/h1&gt;
&nbsp;
  <span style="color:#006600; font-weight:bold;">&lt;%</span> form_for <span style="color:#0066ff; font-weight:bold;">@user</span>, <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> activate_path<span style="color:#006600; font-weight:bold;">&#40;</span>@user.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#ff3333; font-weight:bold;">:html</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:method</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:post</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>form<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  	<span style="color:#006600; font-weight:bold;">&lt;%</span>= form.<span style="color:#9900CC;">error_messages</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  	<span style="color:#006600; font-weight:bold;">&lt;%</span>= render <span style="color:#ff3333; font-weight:bold;">:partial</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;form&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:locals</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:form</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> form <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">%&gt;</span>
  	<span style="color:#006600; font-weight:bold;">&lt;%</span>= form.<span style="color:#9900CC;">submit</span> <span style="color:#996600;">&quot;Activate&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
  &lt;!-- new file app/views/activations/_form.html.erb --&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= form.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:password</span>, <span style="color:#996600;">&quot;Set your password&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= form.<span style="color:#9900CC;">password_field</span> <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
  &lt;br /&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= form.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:password_confirmation</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= form.<span style="color:#9900CC;">password_field</span> <span style="color:#ff3333; font-weight:bold;">:password_confirmation</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
  &lt;br /&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= form.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:openid_identifier</span>, <span style="color:#996600;">&quot;Or use OpenID instead of your email / password&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= form.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:openid_identifier</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;</pre></td></tr></table></div>




<h2>The End</h2>

<p>And that&#8217;s it! Let me know if you have any suggestions for improvement.</p>]]></content:encoded>
			<wfw:commentRss>http://www.claytonlz.com/index.php/2009/07/authlogic-account-activation-tutorial/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Test Driven Development Talk with ASUSoDA</title>
		<link>http://www.claytonlz.com/index.php/2009/04/test-driven-development-talk-with-asusoda/</link>
		<comments>http://www.claytonlz.com/index.php/2009/04/test-driven-development-talk-with-asusoda/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 05:14:31 +0000</pubDate>
		<dc:creator>Clayton</dc:creator>
				<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[Social Networking]]></category>

		<guid isPermaLink="false">http://www.claytonlz.com/?p=255</guid>
		<description><![CDATA[Tonight I had the privilege of giving a presentation on Test Driven Development to the "ASUSoDA":http://asusoda.com group at ASU.  I focused on a higher level aspect of testing and the benefits and pitfalls of TDD. After my planned presentation I was also able to demo my "Intern Management App":http://github.com/clayton/cucumber-demo/tree/master which was fun given the lighthearted nature of the examples.]]></description>
			<content:encoded><![CDATA[<p></p><p>Tonight I had the privilege of giving a presentation on Test Driven Development to the <a href="http://asusoda.com"><span class="caps">ASUS</span>oDA</a> group at <span class="caps">ASU. </span> I focused on the higher level aspects of testing and the specific benefits and pitfalls of <span class="caps">TDD.</span> After my planned presentation I was also able to demo my <a href="http://github.com/clayton/cucumber-demo/tree/master">Intern Management App</a> which was fun given the lighthearted nature of the examples.</p>

<p>Here are the slides from my presentation: <a href="http://www.claytonlz.com/wp-content/uploads/2009/04/asusoda_tdd_20090427.pdf">852k <span class="caps">PDF</span></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.claytonlz.com/index.php/2009/04/test-driven-development-talk-with-asusoda/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>
