How I BDD: Part 1

I've been loving Behavior Driven Development pretty hard. This post made me think of the way I usually approach it, which led to a deep inner need to document that approach.

So, for example, let's say I already have an app created, and I want to add a new resource: Biscuits!

I don't start by creating a Biscuit model, or even a Biscuit model spec. I think about what my application should do regarding biscuits at the highest possible level. Well, for starters, there should be a biscuits index page. So I fire up autotest and start with a controller spec:

# in spec/controllers/biscuits_controller_spec.rb:

require File.dirname(__FILE__) + '/../spec_helper'

describe BiscuitsController, "GET index" do
  it "should be successful" do
    get 'index'
    response.should be_success
  end
end

Of course, autotest complains:

`load_missing_constant': uninitialized constant BiscuitsController

Ok, I need a controller. Easy enough to fix:

 $: script/generate controller Biscuits

Sweet! Autotest is actually running the specs now, but there's a failure:

ActionController::RoutingError in 'BiscuitsController GET index should be successful'
No route matches {:action=>"index", :controller=>"biscuits"}

Note: You may not get this failure if you have a :controller/:action/:id route.

I can fix that one by adding the resource to my routes:

# in config/routes.rb:

ActionController::Routing::Routes.draw do |map|
  ...
  map.resources :biscuits
  ...
end

Looks like we fixed that problem, because autotest is showing a new failure:

ActionController::UnknownAction in 'BiscuitsController GET index should be successful'
No action responded to index

Looks like I need an index action. Alrighty:

# in app/controllers/biscuits_controller.rb:

class BiscuitsController < ApplicationController
  def index
  end
end

Awesome! All specs were successful! Now I move on to what the biscuit index should actually do. The answer to that is "lots of things", but I start with the most basic: list all the biscuits. Still thinking about behavior at the highest possible level, I'd say that means my index view should display each existing biscuit title in a table cell. That means I need a view spec!

I'm realizing now that this post is going to be in multiple parts. I'll start the view spec in the next post. Sorry to leave you hanging. Please don't hate me.

Tags: bdd rails rspec  Meta: 0 comments, permalink

Comments

Leave a response

Comment