Following up on yesterday's simple form-by-example, today I get to fill out that form with Webrat.
Yay! Good clean fun.
In the site feedback Cucumber scenario, I am seeing the feedback form. Next up is "When I fill out the form with effusive praise", which I can define with:
When /^I fill out the form with effusive praise$/ doThat's it! Webrat is just awesome.
fill_in "Name", :with => "Bob"
fill_in "Email", :with => "email@example.com"
fill_in "Subject", :with => "Your site is awesome!"
fill_in "Message", :with => "The recipes are delicious and your use of CouchDB is impressive."
This works because I have wisely wrapped my
<label>tags around the input fields:
<form action='email'>For the uninitiated,
<input name='name' size='30' type='text' />
<input name='email' size='30' type='text' />
<input name='subject' size='50' type='text' />
<textarea cols='55' name='message' rows='8'></textarea>
<input name='Send Comments' type='submit' />
<label>tags not only cue Webrat which form fields to complete, but also activate the HTML form field when the label text is clicked—especially handy when clicking those tiny radio buttons or checkboxes.
I could also have added
idattributes to the various form fields (e.g.
<input id="email-input" type="text"…/>) and then done the label with a
<label for="email-input">Email</label>). That would have the same effect in most browsers, but would have the added benefit of working in Internet Explorer (wrapping
<label>tags does not work in Internet Explorer), but why make life easier for Internet Explorer users?
With that step marked as complete, it is time to do something in response to submitting the feedback form. I drive-by-example a simple "Thanks for the feedback" page. Before I am really done, I need to actually do something with the feedback. In the various legacy versions of EEE Cooks, all that we do with the feedback is send an email. According to the Sinatra FAQ, that means Pony:
cstrom@jaynestown:~/repos/eee-code$ gem install ponyPony is a great little gem—it does just enough to make sending email simple. The RSpec example that I use to drive sending email is:
Building native extensions. This could take a while...
Successfully installed tmail-126.96.36.199
Successfully installed pony-0.3
2 gems installed
it "should send us an email" doThe actual implementation:
with(hash_including(:subject => "Subject"))
:name => "Bob",
:subject => "Subject",
:message => "Feedback message."
post '/email' doThat is a good stopping point for today. Tomorrow, I will work my way back out to the Cucumber scenario.
message = <<"_EOM"
Pony.mail(:to => "us _at_ eeecooks.com".gsub(/\s*_at_\s*/, '@'),
:subject => params[:subject],
:body => message)