Sunday, October 19, 2008

Cucumber scenarios need a title

So I'm playing with Behavior Driven Development (BDD) and Ruby on Rails using Cucumber and Webrat. I thought I'd start with the simplest feature I could think of, "I want to see the application name in the title". So, off we go with features/site_layout.feature:
  1. Feature: Site Layout  
  2.   In order to build familiarity  
  3.   As a user  
  4.   I want to see the application name in the title  
  5.  
  6.   Scenario:  
  7.     Given I am on the home page  
  8.     Then the title tag should be "WOW App"  
I create features/steps/site_steps.rb:
  1. Given /^I am on (.*)$/ do |page|  
  2.   visits case page  
  3.          when "the home page"  
  4.            "/"  
  5.          else  
  6.            raise "Can't find mapping from \"#{page}\" to a path"  
  7.          end  
  8. end  
  9.   
  10. Then /^the (.*) tag should be "(.*)"$/ do |tag, content|  
  11.   response.should have_tag(tag, content)  
  12. end  
And run it with "rake features". What I get is:
C:\dev\temp>rake features
(in C:/dev/temp)
Feature: Site Layout  # features/site_layout.feature
  In order to build familiarity
  As a user
  I want to see the application name in the title
  Scenario: Given I am on the home page     # features/site_layout.feature:6
    Then the title tag should be "WOW App"  # features/steps/site_steps.rb:10
      You have a nil object when you didn't expect it!
      The error occurred while evaluating nil.content_type (NoMethodError)
      ...
      ./features/steps/site_steps.rb:11:in `Then /^the (.*) tag should be "(.*)"$/'
      features/site_layout.feature:8:in `Then the title tag should be "WOW App"'


1 steps failed
rake aborted!
Not quite what I had in mind, I was expecting a failure, but got an error. Nevermind, this is all very new to me, so I'll push on and it'll all become clear right? I create an app/views/layouts/application.html.erb:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
  2.        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.   
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  
  5.   <head>  
  6.   <meta http-equiv="content-type" content="text/html;charset=UTF-8" />  
  7.     <title>WOW App</title>  
  8.   </head>  
  9.   <body>  
  10.   </body>  
  11. </html>  
Run the feature again ... no joy, same error. Hours of web searching leads me nowhere. Finally while looking at some examples I noticed that my Scenario doesn't have a description, so I add one on line 6:
  1. Feature: Site Layout  
  2.   In order to build familiarity  
  3.   As a user  
  4.   I want to see the application name in the title  
  5.  
  6.   Scenario: On the home page  
  7.     Given I am on the home page  
  8.     Then the title tag should be "WOW App"  
Now I get:
C:\dev\temp>rake features
(in C:/dev/temp)
Feature: Site Layout  # features/site_layout.feature
  In order to build familiarity
  As a user
  I want to see the application name in the title
  Scenario: On the home page                # features/site_layout.feature:6
    Given I am on the home page             # features/steps/site_steps.rb:1
      No route matches "/" with {:method=>:get} (ActionController::RoutingError)
...
      features/site_layout.feature:7:in `Given I am on the home page'
    Then the title tag should be "WOW App"  # features/steps/site_steps.rb:10


1 steps failed
1 steps skipped
rake aborted!
A quick addition to config/routes.rb:
  1. map.root :controller => 'example'  
And a stub controller from "ruby script\generate controller Example index", I get:
C:\dev\temp>rake features
(in C:/dev/temp)
Feature: Site Layout  # features/site_layout.feature
  In order to build familiarity
  As a user
  I want to see the application name in the title
  Scenario: On the home page                # features/site_layout.feature:6
    Given I am on the home page             # features/steps/site_steps.rb:1
    Then the title tag should be "WOW App"  # features/steps/site_steps.rb:10


2 steps passed
Hurrah!

No comments :

Post a Comment