<?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>Sarah Mei &#187; cucumber</title>
	<atom:link href="http://www.sarahmei.com/blog/tag/cucumber/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sarahmei.com/blog</link>
	<description></description>
	<lastBuildDate>Sun, 22 Jan 2012 20:16:25 +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>Running Cucumber Features Without a Display</title>
		<link>http://www.sarahmei.com/blog/2010/12/17/cucumber-without-a-display/</link>
		<comments>http://www.sarahmei.com/blog/2010/12/17/cucumber-without-a-display/#comments</comments>
		<pubDate>Fri, 17 Dec 2010 07:41:30 +0000</pubDate>
		<dc:creator>sarahmei</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[ci]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[diaspora]]></category>
		<category><![CDATA[selenium]]></category>

		<guid isPermaLink="false">http://www.sarahmei.com/blog/?p=764</guid>
		<description><![CDATA[I&#8217;ve been helping out with the Diaspora project, an open source social network that gives you control over your own data. When I first started poking around the codebase a few months ago, they&#8217;d just started writing a few cucumber selenium integration tests &#8211; which of course I want to encourage! &#8211; but they weren&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been helping out with <a href="http://joindiaspora.com" target="_blank">the Diaspora project</a>, an <a href="http://github.com/diaspora/diaspora" target="_blank">open source</a> <a href="http://www.sarahmei.com/blog/2010/07/25/safe-facebooking/" target="_blank">social network</a> that gives you control over your own data.</p>
<p>When I first started poking around the codebase a few months ago, they&#8217;d just started writing a few <a href="http://cukes.info" target="_blank">cucumber</a><a></a> <a href="http://seleniumhq.org" target="_blank">selenium</a> integration tests &#8211; which of course <a href="http://www.sarahmei.com/blog/2010/05/29/outside-in-bdd/" target="_blank">I want to encourage!</a> &#8211; but they weren&#8217;t running them on their <a href="http://ci.joindiaspora.com" target="_blank">continuous integration box</a>.  And if you aren&#8217;t running them on CI, you can&#8217;t really call them tests&#8230;</p>
<p>It&#8217;s not that the developers were lazy; it&#8217;s just that Diaspora&#8217;s CI box is an ubuntu server instance that doesn&#8217;t even have xwindows. There&#8217;s no way to attach a display, and without a display, a browser won&#8217;t run. So how can you run selenium features that have to actually open a browser window and click on stuff?<br />
<span id="more-764"></span><br />
<strong>I&#8217;m glad you asked!</strong></p>
<p>You set up a virtual framebuffer &#8211; essentially a simulated display &#8211; and run your features in there. Here&#8217;s how I got this going.</p>
<h3>Step 1: Install Crap</h3>
<p>Yeah, you need xwindows, and a bunch of other stuff. I&#8217;m not a frequent linux user, so I was surprised that a lot of these commands appeared to fail, with extremely lengthy output about how such-and-such dependency could not be installed, or compiled, or resolved, or kafloozled, or whatever. But then when I ran the programs that supposedly weren&#8217;t installed, they worked fine. &#8230;thanks, ubuntu?</p>
<p><code> </code></p>
<pre><code>sudo aptitude install xubuntu-desktop
sudo aptitude install exaile
sudo aptitude install gconf2
sudo aptitude install xvfb
sudo aptitude install firefox
</code></pre>
<p><code> </code></p>
<h3>Step 2: Verify Your Virtual Framebuffer</h3>
<p>I like the phrase &#8220;virtual framebuffer.&#8221; It&#8217;s very computer science, and using it makes me feel smart. But really, it&#8217;s just a display that gets rendered in memory, but not output to any monitor. Programs that require the ability to create windows, such as Firefox, need a display to run in &#8211; but they&#8217;ll take any old display. If you give Firefox a virtual framebuffer, it&#8217;s perfectly happy and treats it like any other display, even though no one can see it.</p>
<p>So let&#8217;s verify we have everything set up to run one. This magic incantation starts one on display port :99.</p>
<p><code> </code></p>
<pre><code> Xvfb :99 -ac -screen 0 1024x768x16
</code></pre>
<p><code> </code></p>
<p>In another shell, run this one, which opens Firefox to example.org inside the virtual display you&#8217;re running in the original shell.</p>
<p><code> </code></p>
<pre><code>DISPLAY=:99.0 firefox http://example.org
</code></pre>
<p><code> </code></p>
<p>Firefox is now open to example.org, the world&#8217;s most boring webpage. Really! Let&#8217;s prove it by taking a screenshot in a third shell.</p>
<p><code> </code></p>
<pre><code>xwd -root -display :99.0 -out xwdout
</code></pre>
<p><code> </code></p>
<p>Your screenshot is now in a file called xwdout. Sadly, this is not an image format that normal image viewers can see, so let&#8217;s convert it to a jpeg.</p>
<p><code> </code></p>
<pre><code>convert xwdout screenshot.jpg
</code></pre>
<p><code> </code></p>
<p>Using your favorite sftp program, retrieve screenshot.jpg and have a look at it. It should look something like this:</p>
<div id="attachment_894" class="wp-caption aligncenter" style="width: 1003px"><a href="http://www.sarahmei.com/blog/wp-content/uploads/2010/12/screenshot1.jpg"><img class="size-full wp-image-894" title="screenshot" src="http://www.sarahmei.com/blog/wp-content/uploads/2010/12/screenshot1.jpg" alt="Yup, a screenshot." width="993" height="282" /></a><p class="wp-caption-text">That boxy UI, those icons from 1995...it could only be linux.</p></div>
<p>Ta-da! You have evidence of this so-called &#8220;virtual&#8221; framebuffer. Now all we have to do is set it up so to start before we run the tests, and stop once the tests are done.</p>
<h3>Step 3: Adventures in Shell Scripting</h3>
<p>This goes in your /etc/init.d, and will give you ULTIMATE POWER, if ultimate means you can start and stop a virtual framebuffer on display port :99.</p>
<p><script src="https://gist.github.com/744561.js"></script> </p>
<p>Now you just need a rake task that starts the display, passes the port info to the tests, and then stops it.  </p>
<p><script src="https://gist.github.com/744607.js?file=gistfile1.rb"></script></p>
<p>And just in case you have to get this running with <a href="http://rvm.beginrescueend.com" target="_blank">rvm</a>, here&#8217;s the shell script that <a href="http://cruisecontrolrb.thoughtworks.com/" target="_blank">cc.rb</a> calls to kick off a Diaspora build on ruby 1.8.7.</p>
<p><script src="https://gist.github.com/744612.js?file=gistfile1.txt"></script></p>
<p>I&#8217;ve had to set this up twice, now, but there are a lot of moving parts. If you try this and notice something I forgot, please let me know.</p>
<p>Enjoy your headless cucumbers&#8230;</p>
<p><span style="color: #b2b2b2;">(I wonder what kind of google search results <strong>that</strong> will get me.)</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sarahmei.com/blog/2010/12/17/cucumber-without-a-display/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Outside-In BDD: How?!</title>
		<link>http://www.sarahmei.com/blog/2010/05/29/outside-in-bdd/</link>
		<comments>http://www.sarahmei.com/blog/2010/05/29/outside-in-bdd/#comments</comments>
		<pubDate>Sun, 30 May 2010 03:14:55 +0000</pubDate>
		<dc:creator>sarahmei</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rspec]]></category>

		<guid isPermaLink="false">http://www.sarahmei.com/blog/?p=443</guid>
		<description><![CDATA[I use rspec on every project, and I&#8217;ve started adding cucumber to all my projects in the last few months. There&#8217;s lots of information out there about how to set up and use cucumber, but there isn&#8217;t much covering your developer workflow when you&#8217;re using these tools. How do you start, and how do you [...]]]></description>
			<content:encoded><![CDATA[<p>I use <a href="http://rspec.info/">rspec</a> on every project, and I&#8217;ve started adding <a href="http://cukes.info">cucumber</a> to all my projects in the last few months. There&#8217;s lots of information out there about how to set up and use cucumber, but there isn&#8217;t much covering your developer workflow when you&#8217;re using these tools.</p>
<p>How do you start, and how do you know you&#8217;re finished? What do you test, and where? These questions can be answered hundreds of different ways, but here&#8217;s my way.<br />
<span id="more-443"></span></p>
<h2>The first code I write: a feature</h2>
<p>As a developer, rather than a designer, I&#8217;m always tempted to start with unit tests and work out towards a cucumber feature (&#8220;inside-out&#8221; testing). But that approach gets me into no end of trouble. I usually end up writing and testing stuff on the model that I don&#8217;t ultimately need. Plus once I&#8217;m down in the weeds coding, I lose track of the big picture.</p>
<p>So I like to do outside-in testing instead. I start each story I get from <a href="http://pivotaltracker.com">tracker</a> with a cucumber feature that expresses how the PM will be able to accept it when I&#8217;m done. The feature helps me frame the problem properly, and focus on doing exactly what I need to make it work. Since I come back to it periodically while I&#8217;m coding, I keep focused on the higher-level goal. And finally &#8211; if I write it first, I can&#8217;t skip writing it once I&#8217;m done.</p>
<h2>Before we get going&#8230;</h2>
<p>There are certain types of tests I don&#8217;t write in this example (and in some cases, at all). Let&#8217;s get those out of the way so you don&#8217;t have to come up with a scathing comment at the bottom of the post.</p>
<ul>
<li><strong>Model tests.</strong> In this example, my model doesn&#8217;t do anything other than default ActiveRecord behavior, so it doesn&#8217;t need any tests. <strong>Don&#8217;t test rails internals.</strong> Once my model has custom behavior, it will have specs, too.</li>
<li><strong>View tests.</strong> I have no tests that verify that my markup is what I expect. That&#8217;s because they&#8217;re a waste of time. Yes, even with complex views. Verify behavior with cucumber tests, unit-test Javascript with <a href="http://github.com/pivotal/jasmine">jasmine</a>, and leave the rest to the humans. You&#8217;ll waste more developer time maintaining them than it would take humans to verify them. Verifiers are a whole lot cheaper than developers.</li>
<li><strong>Error case tests.</strong> In this example, there are no error cases. The model has no validations, and the table has no constraints. Once there are error cases, I generally put those in the model if I can, in the controller when I have to, and never in the cucumber tests. The latter is mostly a suite-speed consideration &#8211; cucumber tests run much more slowly than rspec. Cucumber&#8217;s great for for happy path tests; I leave the rest to rspec.</li>
</ul>
<p>Let&#8217;s get going!</p>
<h2>The first feature</h2>
<p>Say I&#8217;m doing a library app and the first story is &#8220;User can enter a new book into the system.&#8221; Before I write any other code, I write this feature:</p>
<pre lang="RUBY">
Feature: User manages books
  Scenario: User adds a new book
    Given I go to the new book page
    And I fill in "Name" with "War &#038; Peace"
    And I fill in "Description" with "Long Russian novel"
    When I press "Create"
    Then I should be on the book list page
    And I should see "War &#038; Peace"
</pre>
<h2>Starting the fail-fix cycle</h2>
<p>I run it using <code>cucumber features</code>, and it fails on the first line &#8211; <code>Given I go to the new book page</code> &#8211; because cucumber doesn&#8217;t know where the &#8220;new book page&#8221; is. So I add that to the cucumber paths helper.</p>
<pre lang="RUBY">
    when /the new book page/
      new_book_path
</pre>
<p>Now when I run cucumber, it fails because it can&#8217;t find <code>new_book_path</code>. So I add that to <code>routes.rb</code>:</p>
<pre lang="RUBY">
  map.resources :books, <img src='http://www.sarahmei.com/blog/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> nly => [:new]
</pre>
<p>Now when I run cucumber, it complains that it can&#8217;t find the BooksController. That means it&#8217;s time to dive down to rspec controller tests. </p>
<h2>My first spec experience</h2>
<p>I create <code>books_controller_spec.rb</code> in spec/controllers, and add a test for the <code>new</code> method:</p>
<pre lang="RUBY">
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper.rb'))
describe BooksController do
  describe "#new" do
    it "should be successful" do
      get :new
      response.should be_success
    end
  end
end
</pre>
<p>When I run this spec, it complains that there is no BooksController. Fixed:</p>
<pre lang="RUBY">
class BooksController < ApplicationController
end
</pre>
<p></code><br />
I re-run the spec and get "no action responded to new." So I add the <code>new</code> method.</p>
<pre lang="RUBY">
class BooksController < ApplicationController
  def new
  end
end
</pre>
<p>Now the spec passes! Time to check back with cucumber.</p>
<h2>Getting past the first line</h2>
<p>I read through my cucumber feature again:</p>
<pre lang="RUBY">
Feature: User manages books
  Scenario: User adds a new book
    Given I go to the new book page
    And I fill in "Name" with "War &#038; Peace"
    And I fill in "Description" with "Long Russian novel"
    When I press "Create"
    Then I should be on the book list page
    And I should see "War &#038; Peace"
</pre>
<p>Last time I ran it, it failed on the first line because it couldn't find the BooksController. This time, same location, but it says it can't find the view. So whiny! To placate it, I create an empty view called <code>new.html.erb</code> and run it again.</p>
<p>Now cucumber gets past line 1 (huzzah!!) and fails on line 2 (<code>And I fill in "Name" with "War &amp; Peace"</code>) with the message that it can't find a field called Name to fill in. So I add a standard rails form to the view.</p>
<pre lang="RUBY">
<%- form_for @book do |f| -%>
    <%= f.label :name %>
    <%= f.text_field :name %>
    <%= f.label :description %>
    <%= f.text_area :description %>
    <%= f.submit "Create" %>
<%- end -%>
</pre>
<p>Uh oh. Cucumber is mad at me because there is no <code>@book</code> object. Back to rspec for me!</p>
<h2>rspec: The Return</h2>
<p>In my controller's <code>new</code> method, I need to create a book object that the form will use. I first add a test for that in the controller spec:</p>
<pre lang="RUBY">
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper.rb'))
describe BooksController do
  describe "#new" do
    before do
      get :new
    end
    it "should be successful" do
      response.should be_success
    end
    it "should create a book object" do
      assigns(:book).should_not be_nil
    end
  end
end
</pre>
<p>This fails the right way - it says assigns(:book) is nil. So then I add the creation of the book object to the controller.</p>
<pre lang="RUBY">
class BooksController < ApplicationController
  def new
    @book = Book.new
  end
end
</pre>
<p>Now the spec fails, saying it can't find the Book class. It has a point - I haven't created the model yet. Fixed:</p>
<pre lang="RUBY">
class Book < ActiveRecord::Base
end
</pre>
<p>Now it fails saying it can't find the books table. So I write a migration that creates that.</p>
<pre lang="RUBY">
class CreateBooksTable < ActiveRecord::Migration
  def self.up
    create_table :books do |t|
      t.string :name
      t.text :description
    end
  end
  def self.down
    drop_table :books
  end
end
</pre>
<p>Once I do <code>rake db:migrate</code> and <code>rake db:test:prepare</code>, I re-run my controller spec....and it passes! Back to the cucumber feature!</p>
<h2>Cucumber...again.</h2>
<p>In our last episode, cucumber was visibly annoyed because there was no <code>@book</code> object for the form to operate on. I run it again to see if it's still sulking.</p>
<p>Yep. This time it tells me that it can't find books_path. <code>form_for</code> tries to submit to the create path by default, which I haven't added yet. I add it to the routes.</p>
<pre lang="RUBY">
  map.resources :books, <img src='http://www.sarahmei.com/blog/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> nly => [:new, :create]
</pre>
<p>This time, when I run cucumber, it gets through the first three lines (woo hoo!) and fails on the 4th, saying no action responded to create. Back to the rspec-cave, batman!</p>
<h2>rspec: The Sequel to The Return</h2>
<p>I add a controller spec for the <code>create</code> method.</p>
<pre lang="RUBY">
  describe "#create" do
    it "should create a new book" do
      post :create, "book" => {"name" => "Jane Eyre", "description" => "Something Victorian"}
      assigns(:book).should_not be_nil
      assigns(:book).name.should == "Jane Eyre"
    end
  end
</pre>
<p>When I run it, I get the same message as in cucumber: no action responded to create. So I create the create:</p>
<pre lang="RUBY">
class BooksController < ApplicationController
  def new
    @book = Book.new
  end
  def create
  end
end
</pre>
<p>Now when I re-run the spec, it fails saying that assigns(:book) is nil, which makes sense. I put in the guts of <code>create</code> to make that pass.</p>
<pre lang="RUBY">
  def create
    @book = Book.new(params[:book])
    @book.save
  end
</pre>
<p>Now rspec passes! Back to cucumber. </p>
<h2>So...cucumber. We meet again.</h2>
<p>When I re-run the feature, it says I'm missing a template for create, which is correct. However, in this case, I don't want to make a template for create - I want to redirect to the book list page. So once again, I'm back with rspec.</p>
<h2>rspec: Back so soon?</h2>
<p>I add that expectation to the controller spec for <code>create</code>.</p>
<pre lang="RUBY">
    it "should redirect to the book list page" do
      response.should redirect_to books_path
    end
</pre>
<p>It fails saying there's no redirect. So to make it pass, I add a redirect to the controller code.</p>
<pre lang="RUBY">
  def create
    @book = Book.new(params[:book])
    if @book.save
      redirect_to books_path
    end
  end
</pre>
<p>Now my controller specs pass. Cucumber, I'm coming for you!</p>
<h2>Oh, you again.</h2>
<p>Last time, we got through the first 3 lines of the feature and failed on line 4 (<code>When I press "Create"</code>). When I run it this time, it gets through the same 3 lines and then fails in the same place again, saying that no action responded to index. I add <code>index</code> to the routes.</p>
<pre lang="RUBY">
  map.resources :books, <img src='http://www.sarahmei.com/blog/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> nly => [:new, :create, :index]
</pre>
<p>I re-run the feature and get the same error message. WTF, cucumber?! It turns out that rails' implementation of REST uses the same path helper for create and index, so the path helper for <code>index</code> already exists, even though the method does not. A little strange, I know. But we need an <code>index</code> method, so it's back to rspec.</p>
<h2>rspec: For the first time, for the last time...</h2>
<p>I write a spec for the <code>index</code> method.</p>
<pre lang="RUBY">
  describe "#index" do
    it "should be successful" do
      get :index
      response.should be_success
    end
  end
</pre>
<p>I still get no action responded to index. So l add the method in BooksController, empty to start.</p>
<pre lang="RUBY">
  def index
  end
</pre>
<p>Specs pass, back to cucumber!</p>
<h2>How can I miss you if you won't go away?</h2>
<p>Cucumber tells me there's no template for index. So I create an empty one, and re-run. This run, for the first time, I pass line 4 (yaaaaay) but then it fails on line 5 (<code>Then I should be on the book list page</code>) because it can't figure out what I mean by "the book list page." That goes in the cucumber path helper.</p>
<pre lang="RUBY">
    when /the book list page/
      books_path
</pre>
<p>OMG five out of six steps pass! Now cucumber says it can't find "War &amp; Peace" on the page, so let's make the index view list the existing books. Back to rspec...</p>
<h2>Don't go away mad...just go away.</h2>
<p>I add the following <code>it</code> block to the spec for <code>index</code>.</p>
<pre lang="RUBY">
    it "should assign a list of existing books" do
      Book.create!(:name => "Endymion", :description => "weird")
      get :index
      assigns(:books).should_not be_nil
      assigns(:books).length.should == 1
    end
</pre>
<p>It fails because I'm not creating @books in the controller, so I fix that.</p>
<pre lang="RUBY">
  def index
    @books = Book.all
  end
</pre>
<p>Now the specs pass - back to cucumber. </p>
<h2>We really have to stop seeing each other like this.</h2>
<p>Cucumber still says it can't find War &amp; Peace, because I haven't added printing out the books to the index view. I'll fix that.</p>
<pre lang="RUBY">
<%- @books.each do |book| -%>
    <strong><%= h book.name %></strong>
    <%= h book.description %>
<%- end -%>
</pre>
<p>Re-run cucumber and ... ta-da! The feature passes! I've done everything I need to call the story done. I have the minimum amount of code I need, because all the code I wrote was driven by the feature. Story: <strong>delivered</strong>!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sarahmei.com/blog/2010/05/29/outside-in-bdd/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

