Friday, May 29, 2009

Simple Meal Attributes

‹prev | My Chain | next›

Next up in the meal details scenario is to actually see some details of the meal. Specifically:
Then I should see the "Focaccia!" title
Cucumber is telling me to use this as starting point:
Then /^I should see the "([^\"]*)" title$/ do |arg1|
pending
end
Little more than that is actually required to verify this step:
Then /^I should see the "([^\"]*)" title$/ do |title|
response.should have_selector("h1", :content => title)
end
That step fails, of course, since the Sinatra action is not pulling back the meal from CouchDB and, even if it was, there is no Haml template to present the CouchDB data. So let's add these...

I implement the body of the show meal action such that it retrieves the meal data from CouchDB:
      it "should request the meal from CouchDB" do
RestClient.
should_receive(:get).
with(/2009-05-13/).
and_return('{"title":"Foo"}')

get "/meals/2009/05/13"
end
With Sinatra pulling the data from CouchDB, it is time to present that data via a Haml view.

For now, I will describe displaying the meal data proper (links to breadcrumbs, other meals and recipes will come later). To display these attributes, I supply each of my examples with:
  before(:each) do
@title = "Meal Title"
@summary = "Meal Summary"
@description = "Meal Description"
assigns[:meal] = @meal = {
'title' => @title,
'summary' => @summary,
'description' => @description
}
end
The specs that describe showing these three meal attributes:
  it "should display the meal's title" do
render("/views/meal.haml")
response.should have_selector("h1", :content => @title)
end

it "should display the meal's summary" do
render("/views/meal.haml")
response.should have_selector("#summary", :content => @summary)
end

it "should display a description of the meal" do
render("/views/meal.haml")
response.should have_selector("#summary + #description",
:content => @description)
end
I still have not decided if I like Haml, but it does make implementing these three examples simple:
%h1
= @meal['title']

#summary
= @meal['summary']

#description
= @meal['description']
Before working my way out, I am also going to note that both the meal summary and description should be wiki-fied. The example for a wiki-fied summary:
  it "should wikify the meal's summary" do
assigns[:meal]['summary'] = "paragraph 1\n\nparagraph 2"
render("/views/meal.haml")
response.should have_selector("#summary p", :content => "paragraph 1")
end
That fails the first time I run it:
1)
'meal.haml should wikify the meal's summary' FAILED
expected following output to contain a <#summary p>paragraph 1</#summary p> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<h1>
Meal Title
</h1>
<div id="summary">
paragraph 1

paragraph 2
</div>
<div id="description">
Meal Description
</div>
</body></html>
./spec/views/meal.haml_spec.rb:28:
Adding a call to my wiki helper makes that pass:
#summary
= wiki @meal['summary']
After following the same path to wiki-fy the meal description, it is time to work my way back out to Cucumber to verify that I have put everything together properly:
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!" meal enjoyed on March 3, 2009
And a "Focaccia" recipe from March 3, 2009
When I view the "Focaccia!" meal
Then I should see the "Focaccia!" title
And I should see a link to the "Focaccia" recipe in the menu
expected following output to contain a <a>the "Focaccia" recipe in the menu</a> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<h1>
Focaccia!
</h1>
<div id="summary">
<p>meal summary</p>
</div>
<div id="description">
<p>meal description</p>
</div>
</body></html>
(Spec::Expectations::ExpectationNotMetError)
features/browse_meals.feature:42:in `And I should see a link to the "Focaccia" recipe in the menu'
When I click the "March" link
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
8 undefined steps
4 passed steps
Indeed, I have made it past the Meal should display a title step. I am now onto the next step, where it should display a recipe menu item.

That's a good stopping point for tonight. I like stopping on Red in the Red-Green-Refactor cycle. It makes it easy know where to pick up next!
(commit)

No comments:

Post a Comment