Monday, June 15, 2009

Homepage Navigation

‹prev | My Chain | next›

Today, I continue working on the navigation from the homepage Cucumber scenario. I implemented view code to present meal and recipe information last night. I was able to mark one more step in the scenario as complete. Tonight I need to ensure that the homepage is displaying recipe links (as it should after yesterday's effort). The step that will verify that things are working as desired:
Then /^the prominently displayed meals should include the recipe titles$/ do
response.should have_selector(".menu-items",
:content => "Recipe for Meal 1")
When I run the scenario, however, I find:
cstrom@jaynestown:~/repos/eee-code$ cucumber -n features \
-s "Quickly scanning meals and recipes accessible from the home page"
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: Quickly scanning meals and recipes accessible from the home page
Given 25 yummy meals
And 1 delicious recipe for each meal
And the first 5 recipes are Italian
And the second 10 recipes are Vegetarian
When I view the site's homepage
Resource not found (RestClient::ResourceNotFound)
/usr/lib/ruby/1.8/net/http.rb:543:in `start'
./helpers.rb:41:in `recipe_link'
./helpers.rb:27:in `wiki'
./helpers.rb:27:in `gsub!'
./helpers.rb:27:in `wiki'
/home/cstrom/repos/eee-code/views/index.haml:7:in `render'
/home/cstrom/repos/eee-code/views/index.haml:3:in `each'
/home/cstrom/repos/eee-code/views/index.haml:3:in `render'
./features/support/../../eee.rb:30:in `GET /'
(eval):7:in `get'
features/site.feature:13:in `When I view the site's homepage'
Hmm... A RestClient error—time to check the CouchDB log, where I see:
[info] [<0.24918.15>] - - 'GET' /eee-test/2009/06/11/recipe 404
Ah, somehow I am requesting the wrong URL. I ought to be requesting the ID from the DB, not a nested resource as I have done here. Specifically, I should be looking for /eee-test/2009-06-11-recipe. So where am I going wrong?

The request is being made by the wiki helper, which, in turn, uses the recipe_link helper to convert wiki text like [recipe:2009/06/11/recipe] into links to the recipe page—by looking the recipe up in the CouchDB database. In the current Cucumber scenario, I am generating wiki text with slashes (as I think I did in the legacy application). I have the feeling that, when I coded the helper in the first place, I accounted only for explicit IDs (e.g. 2009-06-11-recipe).

So it is back into the helper specification to add this example:
  it "should be able to link with \"slash\" dates (e.g. 2009/06/11/recipe)" do

I am testing implementation details here rather than behavior. Should I ever change from RestClient to CouchRest, I will have to return here to update the spec—even though no behavior will change. I accept this as the price to keeping my unit tests at the unit level. Hopefully I will not be changing CouchDB client libraries often.

At any rate, this example can drive change in the actual code. This version of the recipe_link helper will work with links with slashes or dashes:
    def recipe_link(link, title=nil)
permalink = link.gsub(/\//, '-')
recipe = JSON.parse(RestClient.get("#{_db}/#{permalink}"))
%Q|<a href="/recipes/#{recipe['_id']}">#{title || recipe['title']}</a>|
I can now move back out to the Cucumber scenario to see where I am at:

Happily, where I am at is done with another step. Up next, I will move onto the next step which moves off the homepage as the "user" explores the site.

No comments:

Post a Comment