As a way of learning a new framework I started to write my own blog application using Ruby on Rails. I’ve also taken the opportunity to try a bit of BDD using Cucumber, which I’m finding is a lot of fun. One feature that I have written recently and particularly like is:

Feature: Main Title
  In order to improve SEO and help readers know what they are looking at
  The reader or search engine spider will need the name of the page
  and the author's name in the title of all pages

  Background:
    Given I have a post with title "Why I really like Ruby"

  Scenario Outline: Pages contain name and author in the title
    Given I am on the <Page>
    Then I should see "<Full Title>" within "title"
    Examples:
    | Page                          | Full Title                            |
    | home page                     | Home - Trevor Power                   |
    | about page                    | About - Trevor Power                  |
    | contact page                  | Contact - Trevor Power                |
    | blog page                     | Blog - Trevor Power                   |
    | "Why I really like Ruby" post | Why I really like Ruby - Trevor Power |

This test is very simple, it visits each page in the listed examples and checks that the title is as expected. For me it is important that this is tested automatically because as developer/tester of the site I never really pay attention to the text that appears in the window title. But this text is important for search engines as well as appearing in browser history and book marks.

Another thing I like about this test is the cross cutting nature of it. The titles of all pages from different parts of the website are listed together and any inconsistencies are made obvious.

I don’t have syntax highlighting on this blog for gherkin (the language used in the above spec) but here is a screen shot of the results which might be more readable:

Screen shot of the Cucmber results for the above feature

Tags: , , ,