Tuesday, June 23, 2009

Not Much of a Scenario

‹prev | My Chain | next›

With a little bit of cleanup behind, it's on to the next Cucumber scenario—exploring food categories from the homepage. The whole scenario:



Nice! Out of 12 steps in this scenario, 4 are already passing and 2 more are defined, but not yet reached.

The first undefined step in the scenario, clicking on the Italian category on the homepage, describes something missing on the homepage—category links. So, it is time for an RSpec example for the Haml template:
  it "should link to the listing of Italian recipes" do
render("/views/index.haml")
response.should have_selector("a",
:content => "Italian")
end
I implement that with a call to the categories helper, which links to the Italian category, as well as the other major categories on the site.

That is sufficient to implement the next step, "When I click the Italian category". Hey, that seems vaguely familiar. Have I already done something similar? In fact, I have:
When /^I click on the Italian category$/ do
click_link "Italian"
end
Hmm… I do not particularly care for the "on" that is currently in there. I think that it reads better without it, so I remove it from the text of an earlier scenario and from the step definition. While I am re-working that step anyway, I note that I will soon need to verify that the user can click the "Breakfast" category. To get the step passing for both categories, I use a Regexp matcher:
When /^I click the (\w+) category$/ do |category|
click_link category
end
With that step passing, I am up to the "Then I should see 20 recipes" step. Again, this seems familiar. Looking through the old recipe search feature step definitions, I find:
Then /^I should see (\d+) results$/ do |count|
response.should have_selector("table td a", :count => count.to_i)
end
That is just what I want! Changing the scenario text from "Then I should see 20 recipes" to "Then I should see 20 results", I find:
  Scenario: Exploring food categories (e.g. Italian) from the homepage # features/site.feature:31
Given 25 yummy meals # features/step_definitions/site.rb:1
And 50 Italian recipes # features/step_definitions/recipe_search.rb:119
And 10 Breakfast recipes # features/step_definitions/recipe_search.rb:119
When I view the site's homepage # features/step_definitions/site.rb:84
And I click the Italian category # features/step_definitions/site.rb:128
Then I should see 20 results # features/step_definitions/recipe_search.rb:241
expected following output to contain a <table td a/> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head><title>EEE Cooks</title></head>
<html><body>
<div id="header">
<div id="eee-header-logo">
<a href="/">
<img alt="Home" src="/images/eee_corner.png"></a>
</div>
</div>
<ul id="eee-categories">
<li><a href="/recipes/search?q=category:italian">Italian</a></li>
<li><a href="/recipes/search?q=category:asian">Asian</a></li>
<li><a href="/recipes/search?q=category:latin">Latin</a></li>
<li><a href="/recipes/search?q=category:breakfast">Breakfast</a></li>
<li><a href="/recipes/search?q=category:chicken">Chicken</a></li>
<li><a href="/recipes/search?q=category:fish">Fish</a></li>
<li><a href="/recipes/search?q=category:meat">Meat</a></li>
<li><a href="/recipes/search?q=category:salad">Salad</a></li>
<li><a href="/recipes/search?q=category:vegetarian">Vegetarian</a></li>
<a>Recipes</a>
</ul>
<form action="/recipes/search" method="get">
<input maxlength="2048" name="query" size="31" type="text" value="category:italian"><input name="s" type="submit" value="Search">
</form>
<p class="no-results">
No results matched your search. Please refine your search
</p>
Aw nuts! And I was on such a roll…

Upon further inspection, I note that there were no matches on the Italian recipe category. The source of the trouble was the implementation of the "Given 50 Italian recipes" step. That step, as shown in the Cucumber output, was defined for the recipe search feature. It was defined without categories. The text after the number ("Italian") was used to build the recipe title, not to assign categories. Since that step made no use of the categories / tag_names, I am free to make use of it now:
Given /^(\d+) (.+) recipes$/ do |count, keyword|
date = Date.new(2009, 4, 22)

(1..count.to_i).each do |i|
permalink = "id-#{i}-#{keyword.gsub(/\W/, '-')}"

recipe = {
:title => "#{keyword} recipe #{i}",
:date => date,
:preparations => [
{ 'ingredient' => { 'name' => 'ingredient' } }
],
:tag_names => [keyword.downcase]

}

RestClient.put "#{@@db}/#{permalink}",
recipe.to_json,
:content_type => 'application/json'
end
end
After making similar adjustments for the next several steps, I am on the last step of this scenario—when I search for a category that only has 10 recipes (i.e. is less that the search page size), then I should see no more result pages. I define this step with the assumption that, in such a case, the "Next" link should be inactive:
Then /^I should see no more pages of results$/ do
response.should have_selector(".inactive", :content => "Next »")
end
And, just like that, I have another scenario complete:



There are still a few more scenarios in need of implementation, but I am getting very close to deploying.

No comments:

Post a Comment