Wednesday, June 24, 2009


‹prev | My Chain | next›

The next Cucumber scenario to implement is a somewhat simple feedback form. The very first incarnation of the site was static HTML baked from XML (even many of the search pages were baked). As such, we could not easily do in-page comments (pre-dated prototype.js and jQuery).

Even today, we still prefer the feedback form rather than on-page comments. It is amazing how rude some people can be when commenting on a family cookbook.

The scenario so far:
cstrom@jaynestown:~/repos/eee-code$ cucumber features -n \
-s "Give feedback to the authors of this fantastic site"
Sinatra::Test is deprecated; use Rack::Test instead.
Feature: Site

So that I may explore many wonderful recipes and see the meals in which they were served
As someone interested in cooking
I want to be able to easily explore this awesome site

Scenario: Give feedback to the authors of this fantastic site
Given 25 yummy meals
When I view the site's homepage
And I click "Send us comments"
Could not find link with text or title or id "Send us comments" (Webrat::NotFoundError)
features/site.feature:50:in `And I click "Send us comments"'
Then I should see a feedback form
When I fill out the form with effusive praise
And click the submit button
Then I should see a thank you note

1 scenario
1 failed step
4 undefined steps
2 passed steps
There is nothing in the way of behavior in the addition of a link to a feedback form. Still there is some semantic information in how this should be added to the homepage, so an example is in order. The feedback link should be included as part of an "About Us" section in the right-hand column of the homepage:
  it "should have a recipe search section" do
response.should have_selector(".rhc h1",
:content => "About Us")
I implement this, including the link to the feedback form with the following Haml:
%h1 About Us
%p We are a young family living in the Baltimore area. One day we decided to put our recipe box online. This is our family cookbook
%a{:href => "/feedback"} Send us comments
That gets the "Send us comments" step passing:

The next steps in the Cucumber scenario describe the feeback page, which does not yet exist. That means it is time to start working my way back into the code. First, the Sinatra application needs to respond to a feedback URL:
  describe "GET /feedback" do
it "should respond OK" do
get "/feedback"
last_response.should be_ok
Initially, I implement that with an empty string response (as the simplest thing that can work):
get '/feedback' do
I quickly replace that with a call to haml :feedback, which will need to be driven by example. The first example requires a name field for the feedback form:
  it "should include a field for the user's name" do
response.should have_selector("label",
:content => "Name")
The first time I run the spec, I get an error:
Errno::ENOENT in 'feedback.haml should include a field for the user's name'
No such file or directory - ./views/feedback.haml
/home/cstrom/repos/eee-code/spec/spec_helper.rb:25:in `read'
/home/cstrom/repos/eee-code/spec/spec_helper.rb:25:in `render'
I change the message by creating a feedback.haml template:
cstrom@jaynestown:~/repos/eee-code$ touch views/feedback.haml
cstrom@jaynestown:~/repos/eee-code$ spec ./spec/views/feedback.haml_spec.rb

'feedback.haml should include a field for the user's name' FAILED
expected following output to contain a tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "">


Finished in 0.008351 seconds
Finally, I am done changing the message, now I can make it pass:
%input{:type => "text", :name => "name"}
I follow a similar path to get email address, subject and message fields on this form.

I will call it a night at that point. I will pick up tomorrow implementing the form handler and moving closer to being complete with this scenario.

Before shutting down, I will mark my progress by defining the should-see-a-feedback-form step:
Then /^I should see a feedback form$/ do
response.should have_selector("h1", :content => "Feedback")
response.should have_selector("form")

No comments:

Post a Comment