Wednesday, May 20, 2009

BDD with cucumber, rspec, ruby on rails - 1st in the series - Post 1

In my latest project, I am trying my hand at BDD. I would like to write about how to get started with BDD and write test first then code along the way -
Also, I am working in Agile SCRUM methodology which allows us to talk in terms of stories, tasks, impediments etc

1. I am building a social networking site based on internal data of the organisation - similar to facebookOkay - we have a story as -
Feature: As a user, I should be able to see [first name last name] on my profile
Scenario: As a user, I should be able to see [first name last name] on my profile
Given Manasi Vora is a user
And Manasi logs in to the site
Then Manasi should see Welcome Manasi VoraAnd Manasi should see Name: Manasi Vora
I created a view_person_profile.feature file to list the above and other scenarios.

2. I wrote a view_person_profile_steps.rb file for each of the above Given, When and Then statements
Given /^(.*) (.*) is a user in Profiles$/ do fname, lname
# I don't know what to write here ?? so I need a model to store the user
end

3. I now need a model to store the information of all users, which I created as -
ruby script/generate model Person

Added the required columns to the migration -
first_name
last_name
.
.
.
.

Ran the migration with rake db:migrate

Time for some specs now.. I wrote specs to test if the model is created and returns the first_name and last_name

4. I can now go back and complete my step in the steps.rb file -
Given /^(.*) (.*) is a user in Profiles$/ do fname, lname
@user[fname] = Person.find_by_first_name_and_last_name(fname,lname)
end
5. Next step to be implemented is about allowing the user to login
Continued further in Post 2...

0 comments: