Home > codevil-beyondthebuzzwords-bdd-codebase

codevil-beyondthebuzzwords-bdd-codebase

Codevil-beyondthebuzzwords-bdd-codebase is a project mainly written in RUBY and JAVASCRIPT, it's free.

just the basic code to support my article @ codevil

gems you will need (not to mention all their dependencies)

cucumber, mysql, webrat, rspec, rspec_rails, zentest

creating the application

rails --database=mysql bookstore

creating the databases

rake db:create:all rake db:migrate

adding cucumber support

script/generate cucumber

writing the first feature

gedit features/register_new_books.feature &

executing the features

rake features

Feature: Register new books In order to make my bookstore expansible As a bookstore owner I want to be able to register new books

Scenario Outline: User register a new book successfully # features/register_new_books.feature:6 Given I am on the new book register page # features/step_definitions/webrat_steps.rb:6 Then I should see "Title" # features/step_definitions/webrat_steps.rb:93 And I should see "Author" # features/step_definitions/webrat_steps.rb:93 And I should see "Rating" # features/step_definitions/webrat_steps.rb:93 And I should see "Synopsis" # features/step_definitions/webrat_steps.rb:93 When I fill in "book[title]" with "" # features/step_definitions/webrat_steps.rb:22 And I fill in "book[author]" with "" # features/step_definitions/webrat_steps.rb:22 And I fill in "book[rating]" with "" # features/step_definitions/webrat_steps.rb:22 And I fill in "book[synopsis]" with "" # features/step_definitions/webrat_steps.rb:22 And I press "Save" # features/step_definitions/webrat_steps.rb:14 Then I should be on the new book register page # features/step_definitions/webrat_steps.rb:117 And I should see "Title" # features/step_definitions/webrat_steps.rb:93 And I should see "Rating" # features/step_definitions/webrat_steps.rb:93 And I should see "Author" # features/step_definitions/webrat_steps.rb:93 And I should see "Synopsis" # features/step_definitions/webrat_steps.rb:93 And I should not see "" # features/step_definitions/webrat_steps.rb:97 And I should not see "" # features/step_definitions/webrat_steps.rb:97 And I should not see "" # features/step_definitions/webrat_steps.rb:97 And I should not see "" # features/step_definitions/webrat_steps.rb:97 And I should see "successfully" # features/step_definitions/webrat_steps.rb:93

Examples:
  | book_title       | book_rating | book_synopsis  | book_author         |

CLUE | the god delusion | 9 | kicks gods ass | richard dawkins | ==> Can't find mapping from "the new book register page" to a path. ==> Now, go and add a mapping in /home/macskeptic/workspace/rails/bookstore/features/support/paths.rb (RuntimeError) ==> /home/macskeptic/workspace/rails/bookstore/features/support/paths.rb:22:in /^I am on (.+)$/' CLUE features/register_new_books.feature:7:inGiven I am on the new book register page' | the antichrist | 9 | ubermesnch | friedrich nietzsche | Can't find mapping from "the new book register page" to a path. Now, go and add a mapping in /home/macskeptic/workspace/rails/bookstore/features/support/paths.rb (RuntimeError) /home/macskeptic/workspace/rails/bookstore/features/support/paths.rb:22:in /^I am on (.+)$/' features/register_new_books.feature:7:inGiven I am on the new book register page' | holy bible | 0 | sucks badly | charlatans | Can't find mapping from "the new book register page" to a path. Now, go and add a mapping in /home/macskeptic/workspace/rails/bookstore/features/support/paths.rb (RuntimeError) /home/macskeptic/workspace/rails/bookstore/features/support/paths.rb:22:in /^I am on (.+)$/' features/register_new_books.feature:7:inGiven I am on the new book register page'

3 scenarios (3 failed) 60 steps (3 failed, 57 skipped)

editing the paths to fix the error above (missing "the new book register page")

gedit features/support/paths.rb &

running the features again changes the error

rake features

No route matches "/books/new" with {:method=>:get} (ActionController::RoutingError) (eval):2:in /^I am on (.+)$/' features/register_new_books.feature:7:inGiven I am on the new book register page'

creating the books contoller to fix the error just above

gedit app/controllers/books_controller.rb &

running the features again changes the error

rake features

  expected the following element's content to include "Title":
   (Spec::Expectations::ExpectationNotMetError)
  features/register_new_books.feature:8:in `Then I should see "Title"'

3 scenarios (3 failed) 60 steps (3 failed, 54 skipped, 3 passed)

doing the bare minimum to make the test happy

gedit app/controllers/books_controller.rb &

running the features again changes the error (see, already 15 steps completed successfully)

rake features

  Could not find field: "book[title]" (Webrat::NotFoundError)
  (eval):2:in `/^I fill in "([^"]*)" with "([^"]*)"$/'
  features/register_new_books.feature:12:in `When I fill in "book[title]" with "<book_title>"'

3 scenarios (3 failed) 60 steps (3 failed, 42 skipped, 15 passed)

adding an assertion at the end to ensure that the book has been correctly saved to the database

gedit features/register_new_books.feature &

adding steps specific for the books expectations

gedit features/step_definitions/book_steps.rb &

now, to fix the current error on the feature, we need to create a template

gedit app/views/books/new.html.erb &

to be able to properly create the template we also will need the model, so on to it

script/generate model book title:string author:string rating:decimal synopsis:text rake db:migrate

fix the controller to point to the newly created template

gedit app/controllers/books_controller.rb &

changing the feature to focus on what is really important

gedit features/register_new_books.feature &

let's run the feature again => all form filling worked ;)

rake features

  No action responded to create. Actions: new (ActionController::UnknownAction)
  /usr/lib/ruby/1.8/benchmark.rb:308:in `realtime'
  (eval):2:in `/^I press "([^"]*)"$/'
  features/register_new_books.feature:16:in `And I press "Save"'

3 scenarios (3 failed) 48 steps (3 failed, 18 skipped, 27 passed)

creating a method "create" on the books controller to fix the error above (remember, only do the minimum necessary)

gedit app/controllers/books_controller.rb &

running the specs again gives us a nice outcome

rake features

  TODO (Cucumber::Pending)
  features/register_new_books.feature:22:in `And the book with the title "<book_title>" should have been saved on the database'

3 scenarios (3 pending) 48 steps (3 pending, 45 passed)

to fix the error above, first we need to go back to the book specific steps and define what is a book saved on the database

gedit features/step_definitions/book_steps.rb &

now, running the feature again gives us an error

rake features

  expected nil? to return false, got true (Spec::Expectations::ExpectationNotMetError)
  features/register_new_books.feature:22:in `And the book with the title "<book_title>" should have been saved on the database'

3 scenarios (3 failed) 48 steps (3 failed, 45 passed)

the problem is that our book is not being saved to the database, so, on to it

gedit app/controllers/books_controller.rb &

running the feature one more time... o/

rake features

Feature: Register new books In order to make my bookstore expansible As a bookstore owner I want to be able to register new books

Scenario Outline: User register a new book successfully # features/register_new_books.feature:6 Given I am on the new book register page # features/step_definitions/webrat_steps.rb:6 Then I should see "Title" # features/step_definitions/webrat_steps.rb:93 And I should see "Author" # features/step_definitions/webrat_steps.rb:93 And I should see "Rating" # features/step_definitions/webrat_steps.rb:93 And I should see "Synopsis" # features/step_definitions/webrat_steps.rb:93 When I fill in "book[title]" with "" # features/step_definitions/webrat_steps.rb:22 And I fill in "book[author]" with "" # features/step_definitions/webrat_steps.rb:22 And I fill in "book[rating]" with "" # features/step_definitions/webrat_steps.rb:22 And I fill in "book[synopsis]" with "" # features/step_definitions/webrat_steps.rb:22 And I press "Save" # features/step_definitions/webrat_steps.rb:14 Then I should not see "" # features/step_definitions/webrat_steps.rb:97 And I should not see "" # features/step_definitions/webrat_steps.rb:97 And I should not see "" # features/step_definitions/webrat_steps.rb:97 And I should not see "" # features/step_definitions/webrat_steps.rb:97 And I should see "successfully" # features/step_definitions/webrat_steps.rb:93 And the book with the title "" should have been saved on the database # features/step_definitions/book_steps.rb:1

Examples:
  | book_title       | book_rating | book_synopsis  | book_author         |
  | the god delusion | 9           | kicks gods ass | richard dawkins     |
  | the antichrist   | 9           | ubermesnch     | friedrich nietzsche |
  | holy bible       | 0           | sucks badly    | charlatans          |

3 scenarios (3 passed) 48 steps (48 passed)