Thursday, March 5, 2009

Spike Cucumber and Sinatra

‹prev | My Chain | next›

Continuing the chain, tonight I do a quick spike of how Cucumber might work with Sinatra.

I have zero experience with Cucumber and Sinatra, so for all I know, this is a non-trivial exercise that will require several aborted spikes. Fortunately Google's first hit says otherwise: http://wiki.github.com/aslakhellesoy/cucumber/sinatra.

Before getting started, I need cucumber:
cstrom@jaynestown:~$ gem install cucumber
WARNING: Installing to ~/.gem since /usr/lib/ruby/gems/1.8 and
/usr/bin aren't both writable.
Successfully installed term-ansicolor-1.0.3
Successfully installed polyglot-0.2.5
Successfully installed treetop-1.2.4
Successfully installed cucumber-0.1.16
4 gems installed
Wow, that was nice and fast—yep, I disable rdoc & ri generation in my .gemrc:
cstrom@jaynestown:~$ cat ~/.gemrc
gem: --no-ri --no-rdoc
Also needed is webrat (which also requires a system libxslt1-dev, if not already installed):

cstrom@jaynestown:~/repos/eee-code$ gem install webrat
WARNING: Installing to ~/.gem since /usr/lib/ruby/gems/1.8 and
/usr/bin aren't both writable.
Building native extensions. This could take a while...
Successfully installed nokogiri-1.2.1
Successfully installed webrat-0.4.2
2 gems installed


Following the instructions from the Sinatra/Cucumber page, I add features/support/env.rb to eee-code:
# NOTE: This must come before the require 'webrat', otherwise
# sinatra will look in the wrong place for it's views.
require File.dirname(__FILE__) + '/../../eee-code'

# RSpec matchers
require 'spec/expectations'

# Webrat
require 'webrat'
Webrat.configure do |config|
config.mode = :sinatra
end

World do
Webrat::SinatraSession.new
end
Now what?

To start using cucumber in a Rails app, I would use the Cucumber generator:
./script/generate cucumber
That is not going to help in Sinatra.

So I try running cucumber directly:
cstrom@jaynestown:~/repos/eee-code$ cucumber features -n
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- ./features/support/../../eee-code (LoadError)
Failed to load features/support/env.rb
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from ./features/support/env.rb:3
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /home/cstrom/.gem/ruby/1.8/gems/cucumber-0.1.16/bin/../lib/cucumber/cli.rb:227:in `require_files'
from /home/cstrom/.gem/ruby/1.8/gems/cucumber-0.1.16/bin/../lib/cucumber/cli.rb:225:in `each'
from /home/cstrom/.gem/ruby/1.8/gems/cucumber-0.1.16/bin/../lib/cucumber/cli.rb:225:in `require_files'
from /home/cstrom/.gem/ruby/1.8/gems/cucumber-0.1.16/bin/../lib/cucumber/cli.rb:148:in `execute!'
from /home/cstrom/.gem/ruby/1.8/gems/cucumber-0.1.16/bin/../lib/cucumber/cli.rb:13:in `execute'
from /home/cstrom/.gem/ruby/1.8/gems/cucumber-0.1.16/bin/cucumber:6
from /home/cstrom/.gem/ruby/1.8/bin/cucumber:19:in `load'
from /home/cstrom/.gem/ruby/1.8/bin/cucumber:19
Ew.

Ah, no worries, I misunderstood the Cucumber/Sinatra sample. I should require the application code, not the application directory. Rookie mistake. So I change the require line:
# NOTE: This must come before the require 'webrat', otherwise
# sinatra will look in the wrong place for it's views.
require File.dirname(__FILE__) + '/../../spike'

# RSpec matchers
require 'spec/expectations'

# Webrat
require 'webrat'
Webrat.configure do |config|
config.mode = :sinatra
end

World do
Webrat::SinatraSession.new
end
Then I retry cucumber and, as expected, get a bunch of pending steps:
cstrom@jaynestown:~/repos/eee-code$ cucumber features -n
Feature: See a meal

So that I can see an old meal
As a web user
I want to browse a single meal by permalink
Scenario: View Meal
Given a "Breakfast Elves" meal
When I view the meal permalink
Then the title should include "Breakfast Elves"


1 scenario
3 steps pending (3 with no step definition)

You can use these snippets to implement pending steps which have no step definition:

Given /^a "Breakfast Elves" meal$/ do
end

When /^I view the meal permalink$/ do
end

Then /^the title should include "Breakfast Elves"$/ do
end
That is a dumbed down version of the feature from the other night—dumbed down for tonight's spike purposes.

That's enough for tonight. I ran into some issue with defining the steps, but I think I have them resolved now. I will try finishing that up tomorrow.

No comments:

Post a Comment