Sunday, May 31, 2009

Breadcrumbs

‹prev | My Chain | next›

On tap for tonight are some breadcrumbs for the show meal page. For a meal prepared today (May 31, 2009), there should be breadcrumb links to the list of meals in May, and to the list of meals in 2009.

The link to meals this year, in RSpec notation:
  it "should display a breadcrumb link to the other meals in this year" do
render("/views/meal.haml")
response.should have_selector(".breadcrumbs a",
:href => "/meals/#{@year}")
end
(the @year instance variable comes from the before(:each) block).

To make this example pass, I add the following Haml code:
- date = Date.parse(@meal['date'])
.breadcrumbs
%a{:href => "/meals/#{date.year}"}= date.year
It is debatable if parsing the date string, as I do here, or splitting the date string on dashes (e.g. "2009-05-31".split(/-/)[0]) is the easiest thing to do. I make an educated guess here that things will be easier for me if I have a Date object.

With the year breadcrumb in place, it is time for the month breadcrumb. The RSpec example:
  it "should display a breadcrumb link to the other meals in this month" do
render("/views/meal.haml")
response.should have_selector(".breadcrumbs a",
:href => "/meals/#{@year}/#{"%02d" % @month}")
end
(Note: I'm using the % shortcut for sprintf in that example)

To make that example pass, I make use of the strftime method on the date object (that date object did come in handy):
- date = Date.parse(@meal['date'])
.breadcrumbs
%a{:href => "/meals/#{date.year}"}= date.year
>
%a{:href => "/meals/#{date.strftime("%Y/%m")}"}= date.strftime("%B")
From cheat strftime, the options that are being used:
    %B - The  full  month  name (``January'')
%m - Month of the year (01..12)
%Y - Year with century


With that code work done, I am ready to move on back out to the Cucumber scenario:



I use Cucumber's suggestion to define the next, very simple, step:
When /^I click the "([^\"]*)" link$/ do |text|
click_link text
end
That simple step gets me tantalizingly close to being done with this scenario. There is much blue (not reached, but implemented steps) and only three remaining steps to be implemented (two of them the same):



I implement the next step (Then I should see "Focaccia!" in the list of meals):
Then /^I should see "([^\"]*)" in the list of meals$/ do |title|
response.should have_selector("a", :content => title)
end
Unfortunately, when I run the scenario through Cucumber, I find:
cstrom@jaynestown:~/repos/eee-code$ cucumber features -n \
-s "Browsing a meal on a specific date"
Feature: Browse Meals

So that I can find meals made on special occasions
As a person interested in finding meals
I want to browse meals by date

Scenario: Browsing a meal on a specific date
Given a "Focaccia" recipe from March 3, 2009
Given a "Focaccia!" meal with the "Focaccia" recipe on the menu
When I view the "Focaccia!" meal
Then I should see the "Focaccia!" title
And I should see a "Focaccia" recipe link in the menu
When I click the "March" link
Then I should see "Focaccia!" in the list of meals
expected following output to contain a <a>Focaccia!</a> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<h1>Meals from March 2009</h1>
<div class="navigation">

|

</div>
<div class="meals">

<h2>
<span class="date">2009-03-03</span>
<span class="title">Focaccia!</span>
</h2>
<p>meal summary</p>
<div class="menu"><p><a href="/recipes/2009-03-03-focaccia">Focaccia</a></p></div>
</div>
</body></html>
(Spec::Expectations::ExpectationNotMetError)
features/browse_meals.feature:44:in `Then I should see "Focaccia!" in the list of meals'
When I click the "Focaccia" link
And I click the "2009" link
Then I should see "Focaccia!" in the list of meals
When I click the "Focaccia!" link
And I click the "Focaccia" link
Then I should see the "Focaccia" recipe

1 scenario
1 failed step
5 skipped steps
1 undefined step
6 passed steps
Aw nuts. When I implemented the list of meals, I failed to actually link to the meal. That is a fairly easy thing to resolve. Tomorrow.

I will again leave myself at red in the Red-Green-Refactor cycle to quickly pick up where I leave off today.

No comments:

Post a Comment