[rspec-users] Where to start after writing feature

David Chelimsky dchelimsky at gmail.com
Fri Dec 5 21:08:52 EST 2008


On Fri, Dec 5, 2008 at 3:36 PM, Stephen Veit <srveit at gmail.com> wrote:
> I am trying to develop code from the feature on down.
>
> So I created a brand new rails app. It has no models yet. I wrote a feature:
>
> Feature: Add tasks
>   In order to track website improvements
>   a user
>   wants to add tasks
>
>   Scenario: User adds task
>     Given task "Display Group Rules" does not exist
>     When I visit "/tasks/new"
>     And I fill in "Name" with "Display Group Rules"
>     And I fill in "Description" with "Displays links to edit each group
> rule"
>     And I fill in "CAS Link" with "GroupRulesManager/"
>     And I press "Submit"
>     Then I should end up on the Tasks page
>     And I should see "Task successfully added"
>     And the task "Display Group Rules" should exist
>
> I then run:
>
> rake db:migrate (this creates my development SQLite3 database)
> rake db:test:prepare (this creates my development SQLite3 database)
> rake features (outputs the following:)
>
> Feature: Add tasks  # features/manage_task.feature
>   In order to track website improvements
>   a user
>   wants to add tasks
>   Scenario: User adds task
>     Given task "Display Group Rules" does not exist
>     When I visit "/tasks/new"
>     And I fill in "Name" with "Display Group Rules"
>     And I fill in "Description" with "Displays links to edit each group
> rule"
>     And I fill in "CAS Link" with "GroupRulesManager/"
>     And I press "Submit"
>     Then I should end up on the Tasks page
>     And I should see "Task successfully added"
>     And the task "Display Group Rules" should exist
>
>
> 5 steps skipped
> 4 steps pending
>
> You can use these snippets to implement pending steps:
>
> Given /^task "Display Group Rules" does not exist$/ do
> end
>
> When /^I visit "\/tasks\/new"$/ do
> end
>
> Then /^I should end up on the Tasks page$/ do
> end
>
> Then /^the task "Display Group Rules" should exist$/ do
> end
>
>
> My question is where to go from here. Do I implement the four pending steps,
> e.g:
>
> Given /^task "(.+)" does not exist$/ do |name|
>   task = Task.find_by_name(name)
>   task.destroy if task
> end
>
> Or do I run:
>
>  ./script/generate scaffold_resource task name:string description:text
> cas_link:string
>
> and the rake rspec and fix whatever errors show up there?
>
> What are your thoughts? TIA

Hi Stephen,

The approach I like to take is to follow the guidance of the system
(cucumber + rspec) every step of the way. Right now there are no
passing steps, so I'd write the code for the 1st pending step, run the
features and find the first thing that is missing or failing.

In the example you've provided, I'd very likely write code like what
you wrote for the first Given step. I imagine that running that
feature would produce an error saying that there is nothing named
Task.

At that point I've gone in two different directions. The more purist
approach would be to create the model at this point - not the scaffold
- as that's the only thing you actually need at this moment to
progress.

I've also made the scaffold at this point. Typically I end up cutting
out a bunch of crap I don't need and from time to time I don't clear
out all the crap I don't need, discovering later that I've got excess
code floating around the system. This is the motivation for avoiding
code generation. It's a tradeoff.

Either way, once that step is passing (by creating the model or
scaffold and then running the migrations), I'd move on to the next
step. Just work your way down, one step at a time.

As your doing this, if you're using the rspec generators, you'll be
generating specs as you go. Any time that you need to actually
implement something in the code that is not generated, I'd head to the
specs to drive that additional behaviour out at the object level.

If you follow this with serious discipline, you'll end up with two
concentric cycles that both follow a Red/Green/Refactor cycle, meeting
at the Green points.

One nice visual analogy for this is a two speed bicycle. The lower
gear, the bigger circle, is the Cucumber cycle. You work there until
you have a failing step, at which point you shift into high gear,
which is the smaller circle, the RSpec cycle. At the point that all
specs are passing and the last failing step is now passing, you shift
back to low gear (Cucumber) and work there until you have a failing
step. Rinse, repeat.

This is how I describe this in The RSpec Book, btw, so tell me now if
it doesn't make sense :)

HTH,
David

>
> --
> Stephen Veit
> 314-616-9688
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list