<?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>has_many :follows &#187; BDD</title>
	<atom:link href="http://hasmanyfollows.com/category/bdd/feed/" rel="self" type="application/rss+xml" />
	<link>http://hasmanyfollows.com</link>
	<description>bits of rails code for social apps</description>
	<lastBuildDate>Fri, 16 Dec 2011 08:18:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Getting Cucumber, Authlogic, and Machinist to Play Together</title>
		<link>http://hasmanyfollows.com/2009/09/06/getting-cucumber-authlogic-and-machinist-to-play-together/</link>
		<comments>http://hasmanyfollows.com/2009/09/06/getting-cucumber-authlogic-and-machinist-to-play-together/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 09:15:24 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[BDD]]></category>
		<category><![CDATA[Cucumber]]></category>
		<category><![CDATA[authlogic]]></category>
		<category><![CDATA[machinist]]></category>

		<guid isPermaLink="false">http://hasmanyfollows.com/?p=3</guid>
		<description><![CDATA[Out of the box, Cucumber, Authlogic, and Machinist need a little bit of bridging to get them to work together. This post will cover setting up your Machinist blueprints for your User model, and Cucumber login stories and login steps. It also covers how to use Authlogic&#8217;s current_user method in your Cucumber stories. This post [...]]]></description>
			<content:encoded><![CDATA[<p>Out of the box, Cucumber, Authlogic, and Machinist need a little bit of bridging to get them to work together. This post will cover setting up your Machinist blueprints for your User model, and Cucumber login stories and login steps. It also covers how to use Authlogic&#8217;s current_user method in your Cucumber stories.</p>
<p>This post assumes you already have Cucumber, Authlogic and Machinist setup and bootstrapped. If not, see these links first: <a title="Install Cucumber, Webrat and RSpec" href="http://wiki.github.com/aslakhellesoy/cucumber/ruby-on-rails" target="_blank">Cucumber</a>, <a title="Authlogic setup tutorial" href="http://github.com/binarylogic/authlogic_example" target="_blank">Authlogic</a>, <a title="Machinist install and setup" href="http://github.com/notahat/machinist" target="_blank">Machinist</a>. </p>
<p>When writing your Cucumber stories you will inevitably come across scenarios like the following:</p>
<div class="filepath">features/user.feature</div>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Scenario: User fills in profile
   Given I am a logged in user
   And I am on the My Profile page
   ...</pre></div></div>

<p>How do you setup a logged in user? First, you need create a Machinist blueprint for your user. </p>
<div class="filepath">spec/blueprint.rb</div>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">User.<span style="color:#9900CC;">blueprint</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  username <span style="color:#996600;">'test_user'</span>
  email <span style="color:#996600;">'test@user.com'</span>
  password <span style="color:#996600;">'spacemonkey'</span>
  password_confirmation <span style="color:#996600;">'spacemonkey'</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Next you will need to create step definitions for your login stories. Here are a few handy functions that can make the step of creating a logged in user easier and more module:</p>
<div class="filepath">features/step_definitions/user_steps.rb</div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> user
   <span style="color:#0066ff; font-weight:bold;">@user</span> <span style="color:#006600; font-weight:bold;">||</span>= User.<span style="color:#9900CC;">make</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> login
  user
  visit <span style="color:#996600;">'/login'</span>
  fill_in<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Email&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:with</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> user.<span style="color:#9900CC;">email</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  fill_in<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Password&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:with</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> user.<span style="color:#9900CC;">password</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  click_button<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Login&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Next we can create the login step:</p>
<div class="filepath">features/step_definitions/user_steps.rb</div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>12
13
14
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">Given <span style="color:#006600; font-weight:bold;">/</span>^I am a logged <span style="color:#9966CC; font-weight:bold;">in</span> user$<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span>
   login
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Now the <code>Given I am a logged in user</code> step should pass. </p>
<p>In your other Cucumber step definitions it will come in handy to be able to use Authlogic&#8217;s built in <code>current_user</code> method in your Cucumber steps. Since the <code>current_user</code> method apply to users, you can add it to your <code>user_steps.rb</code> file within a module:</p>
<div class="filepath">features/step_definitions/user_steps.rb</div>

<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="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> UserHelpers
  <span style="color:#9966CC; font-weight:bold;">def</span> current_user_session
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0066ff; font-weight:bold;">@current_user_session</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#9966CC; font-weight:bold;">defined</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>@current_user_session<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@current_user_session</span> = UserSession.<span style="color:#9900CC;">find</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> current_user
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0066ff; font-weight:bold;">@current_user</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#9966CC; font-weight:bold;">defined</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>@current_user<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@current_user</span> = current_user_session <span style="color:#006600; font-weight:bold;">&amp;</span>amp;<span style="color:#006600; font-weight:bold;">&amp;</span>amp; current_user_session.<span style="color:#9900CC;">user</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
World<span style="color:#006600; font-weight:bold;">&#40;</span>UserHelpers<span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>Now you should be able to easily run tests involving the logged in session. For example, to make the <code>And I am on the My Profile page</code> step pass you will have to add the path to your <code>path.rb</code> file. In the path reference we can now make use of Authlogic&#8217;s <code>current_user</code> method:</p>
<div class="filepath">features/support/paths.rb</div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> NavigationHelpers
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> path_to<span style="color:#006600; font-weight:bold;">&#40;</span>page_name<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">case</span> page_name
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006600; font-weight:bold;">/</span>the My Profile page<span style="color:#006600; font-weight:bold;">/</span>
      user_path<span style="color:#006600; font-weight:bold;">&#40;</span>current_user<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>
World<span style="color:#006600; font-weight:bold;">&#40;</span>NavigationHelpers<span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>Another useful step to add to your user steps is one which creates multiple users. The following step can be used to make a specific amount of users:</p>
<div class="filepath">features/step_definitions/user_steps.rb</div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>15
16
17
18
19
20
</pre></td><td class="code"><pre class="rails" style="font-family:monospace;">Given <span style="color:#006600; font-weight:bold;">/</span>^there exists at least <span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span> other user$<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>number<span style="color:#006600; font-weight:bold;">|</span>
   <span style="color:#0066ff; font-weight:bold;">@users</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
   number.<span style="color:#9900CC;">scan</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>\d<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">join</span>.<span style="color:#5A0A0A; font-weight:bold;">to_i</span>.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#0066ff; font-weight:bold;">@users</span><span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span> = User.<span style="color:#9900CC;">make</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>In the end your <code>user_steps.rb</code> file should look like:<br />
<script src="http://gist.github.com/186744.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://hasmanyfollows.com/2009/09/06/getting-cucumber-authlogic-and-machinist-to-play-together/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

