Up next, according to Cucumber (my master), is the "Browse Meals" feature. The first scenario in there is "Browsing a meal in a given year":
Scenario: Browsing a meal in a given yearThis was one of the first scenarios that I wrote and it shows, I think. I have the same "Given" and the order of the steps reads a bit off. Re-organizing the scenario a bit:
Given a "Even Fried, They Won't Eat It" meal enjoyed in 2009
When I view the list of meals prepared in 2009
Then "Even Fried, They Won't Eat It" should be included in the list
Given a "Even Fried, They Won't Eat It" meal enjoyed in 2009
And a "Salad. Mmmm." meal enjoyed in 2008
When I view the list of meals prepared in 2009
Then I should be able to follow a link to the list of meals in 2008
And "Salad. Mmmm." should be included in the list
Scenario: Browsing a meal in a given yearMuch better.
Given a "Even Fried, They Won't Eat It" meal enjoyed in 2009
And a "Salad. Mmmm." meal enjoyed in 2008
When I view the list of meals prepared in 2009
Then the "Even Fried, They Won't Eat It" meal should be included in the list
And the "Salad. Mmmm." meal should not be included in the list
When I follow the link to the list of meals in 2008
Then the "Even Fried, They Won't Eat It" meal should not be included in the list
And the "Salad. Mmmm." meal should be included in the list
My "Givens" are declared at the outset. There are two paths being followed (one for 2009, one for 2008), both starting with "When" declarations. Best of all, the second "When" flows from the first—clicking a link displayed in the first.
Implementing the first two steps can be accomplished via a single
Given
block:Given /^a "([^\"]*)" meal enjoyed in (\d+)$/ do |title, year|The next step, viewing the list of meals in 2009, can be defined as:
date = Date.new(year.to_i, 5, 13)
permalink = "id-#{date.to_s}"
meal = {
:title => title,
:date => date,
:serves => 4,
:summary => "meal summary",
:description => "meal description"
}
RestClient.put "#{@@db}/#{permalink}",
meal.to_json,
:content_type => 'application/json'
end
When /^I view the list of meals prepared in 2009$/ doIt fails, of course, since I have to define the meals action, so into the code I go...
visit("/meals/2009")
response.status.should == 200
end
As with recipes, I write my meal creation / tear down before / after blocks (this ain't no relational DB with fancy transactions):
context "a CouchDB meal" doThe first meals action example is a simple one:
before(:each) do
@date = Date.new(2009, 5, 13)
@title = "Meal Title"
@permalink = "id-#{@date.to_s}"
meal = {
:title => @title,
:date => @date,
:serves => 4,
:summary => "meal summary",
:description => "meal description"
}
RestClient.put "#{@@db}/#{@permalink}",
meal.to_json,
:content_type => 'application/json'
end
after(:each) do
data = RestClient.get "#{@@db}/#{@permalink}"
meal = JSON.parse(data)
RestClient.delete "#{@@db}/#{@permalink}?rev=#{meal['_rev']}"
end
end
describe "GET /meals/YYYY" doThis fails, of course since the action has not been defined. Let's define it and call it a night:
it "should respond OK" do
get "/meals/2009"
response.should be_ok
end
end
get %r{/meals/(\d+)} do |year|(commit)
end
(commit)
Three steps down, 5 to go in the first meal scenario:
Looks like some fun with map-reduce is in store for tomorrow!
No comments:
Post a Comment