A simple example of TDD with Cucumber and RSpec
Ruby JavaScript
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


copyright 2008 Paul Cortens

see the presentations directory for more helpful documents

A basic example of TDD with Cucumber and RSpec

Steps in this document correspond to tags in my git repository

rails cuc_demo
cd cuc_demo
script/generate cucumber
script/generate rspec

*Step 0*

Add the feature

*Step 1*

rake db:migrate
rake db:test:prepare
  (to create db/schema.rb)

rake features
AUTOFEATURE=true autospec

add some matchers

*Step 2*

script/generate rspec_model Postie name:string body:text
rake db:migrate
rake features

*Step 3*

Need to require webrat (so we get the nice stuff like visits '/')

*Step 4*

Set up a root route (it doesn't load the static page)
script/generate rspec_controller Posties new create show
in routes.rb
  map.resources :posties
  map.root :controller => 'posties', :action => 'new'

*Step 5*

describe PostiesController do
  describe "GET 'new'" do
    it "should assign a new postie to the view" do

make it pass

*Step 6*

add the new postie form to app/views/posties/new.html.erb

remove view specs
- I will let cucumber test the views

*Step 7*

still fails - Webrat uses label (i.e. what the customer sees)
update form to have 'nice' labels

*Step 8*

We forgot the button
  Update feature
  Update view

*Step 9*

Implement create action

Rails has the singular of posties as posty
  inflect.irregular 'postie', 'posties'

describe PostiesController do
  describe "POST 'create'" do
    describe "with valid attributes" do
      it "should redirect to the show pastie page" do

This feature doesn't have the negative, so I didn't implement it

*Step 10*
Note how cucumber is rendering Posties#show now (instead of Posties#create)

add our fields to app/views/pasties/show.html.erb

describe PostiesController do
  describe "GET 'show'" do
    it "should assign a the postie to the view" do

*Step 11*

Need to fix our routes
I also did some messing around with the model to get the pretty urls

*Step 12*

Bug in my routes. I was using PUT instead of POST (for create)

*Step 13*

A few more bugs with the params
  Need to have Postie#to_params use the pretty_id

*Step 14*

git rm public/index.html

*Step 15*

Fire up the browser (for the first time)

Add more examples to the feature

*Step 16*

They fail because my code escapes the HTML, but the matchers don't.

*Step 17*

That's it :)