Thursday, March 12, 2009

Cucumber, Merb and CouchDB - Win (Finally)

‹prev | My Chain | next›

Sometimes it really pays to look at the source code.

Recall from yesterday's link in the chain that I was having difficulty reminiscent of http://www.ruby-forum.com/topic/176289. Despite my best efforts to follow the steps described, I was still getting:
Loading /home/cstrom/repos/eee-merb/config/environments/development.rb
Feature: See a meal # features/browse_meals.feature

So that I can see an old meal
As a web user
I want to browse a single meal by permalink
Scenario: View Meal # features/browse_meals.feature:6
Given a "Breakfast Elves" meal # features/steps/browse_meals.rb:1
When I view the meal permalink # features/steps/browse_meals.rb:8
Then the title should include "Breakfast Elves" # features/steps/browse_meals.rb:12
undefined local variable or method `response' for # (NameError)
./features/steps/browse_meals.rb:13:in `Then /^the title should include "(.+)"$/'
features/browse_meals.feature:9:in `Then the title should include "Breakfast Elves"'


1 scenario
2 steps passed
1 step failed
rake aborted!
Rather than posting a follow-up question, I looked at the commit history for the project. There are relatively few around the time of the ruby forum thread, so I poked through and found this commit. The examples in there all contain response_body.should rather than the response.should that I had used in my Sintra spike. So let's give that a try...

From the Top

To eliminate all doubt, I will start from scratch. First, I need to install the gem:
cstrom@jaynestown:~/repos/eee-merb$ gem install jsmestad-merb_cucumber
WARNING: Installing to ~/.gem since /usr/lib/ruby/gems/1.8 and
/usr/bin aren't both writable.
Successfully installed jsmestad-merb_cucumber-0.5.1.3
1 gem installed
Then I remove the old generated Cucumber files:
cstrom@jaynestown:~/repos/eee-merb$ merb-gen cucumber --session-type webrat -d
Loading init file from /home/cstrom/repos/eee-merb/config/init.rb
Loading /home/cstrom/repos/eee-merb/config/environments/development.rb
Generating with cucumber generator:
[DELETED] features/support/env.rb
[DELETED] lib/tasks/cucumber.rake
[DELETED] autotest/cucumber_merb_rspec.rb
[DELETED] features/steps/result_steps.rb
[DELETED] features/authentication/login.feature
[DELETED] features/authentication/steps/login_steps.rb
[DELETED] features/steps/webrat_steps.rb
[DELETED] bin/cucumber
[DELETED] cucumber.yml
And re-generate:
cstrom@jaynestown:~/repos/eee-merb$ merb-gen cucumber --session-type webrat
Loading init file from /home/cstrom/repos/eee-merb/config/init.rb
Loading /home/cstrom/repos/eee-merb/config/environments/development.rb
Generating with cucumber generator:
[ADDED] features/support/env.rb
[ADDED] lib/tasks/cucumber.rake
[ADDED] autotest/cucumber_merb_rspec.rb
[ADDED] features/steps/result_steps.rb
[ADDED] features/authentication/login.feature
[ADDED] features/authentication/steps/login_steps.rb
[ADDED] features/steps/webrat_steps.rb
[ADDED] bin/cucumber
[ADDED] cucumber.yml
I am not using merb-auth, so I delete the generated authentication example:
cstrom@jaynestown:~/repos/eee-merb$ rm -rf features/authentication/
Next up, I add my Before and After CouchDB hooks (see discussion from the other night as to the why). To do this, append to features/support/env.rb the following:
require 'rest_client'
Before do
uri = "http://127.0.0.1:5984#{repository(:default).adapter.uri.path}"
RestClient.put uri, { }
end

After do
uri = "http://127.0.0.1:5984#{repository(:default).adapter.uri.path}"
RestClient.delete uri
end
Remove the db:automigrate dependency from lib/tasks/cucumber.rake by commenting it out on line 16:
dependencies = ['merb_cucumber:test_env'] #, 'db:automigrate']
Finally, define features/browse_meal.feature:
Feature: See a meal

So that I can see an old meal
As a web user
I want to browse a single meal by permalink
Scenario: View Meal
Given a "Breakfast Elves" meal
When I view the meal permalink
Then the title should include "Breakfast Elves"
And the steps in features/steps/browse_meals.rb (using response_body instead of response):
Given /^a "(.+)" meal$/ do |title|
@permalink = title.gsub(/\W/, '-')

Meal.create(:id => @permalink,
:title => title)
end

When /^I view the meal permalink$/ do
visit("/meals/show/#{@permalink}.html")
end

Then /^the title should include "(.+)"$/ do |title|
response_body.should have_selector("h1", :content => title)
end
Running rake features now gives us our desired results:
cstrom@jaynestown:~/repos/eee-merb$ rake features
(in /home/cstrom/repos/eee-merb)
Loading init file from /home/cstrom/repos/eee-merb/config/init.rb
Loading /home/cstrom/repos/eee-merb/config/environments/development.rb
Feature: See a meal # features/browse_meals.feature

So that I can see an old meal
As a web user
I want to browse a single meal by permalink
Scenario: View Meal # features/browse_meals.feature:6
Given a "Breakfast Elves" meal # features/steps/browse_meals.rb:1
When I view the meal permalink # features/steps/browse_meals.rb:8
Then the title should include "Breakfast Elves" # features/steps/browse_meals.rb:12


1 scenario
3 steps passed


Finished in 0.002594 seconds

0 examples, 0 failures

Shutting Down the Spike

I did try re-adding the cucumber task's dependency on db:automigrate back. Unfortunately, I still get the rake borted messages of Unknown property 'views' and couch logs to the effect of:
[Fri, 13 Mar 2009 00:26:39 GMT] [info] [<0.592.0>] HTTP Error (code 404): {not_found,missing}
[Fri, 13 Mar 2009 00:26:39 GMT] [info] [<0.592.0>] 127.0.0.1 - - "GET /eee-test/_design/Merb::DataMapperSessionStore" 404
[Fri, 13 Mar 2009 00:26:39 GMT] [info] [<0.593.0>] HTTP Error (code 404): {not_found,missing}
[Fri, 13 Mar 2009 00:26:39 GMT] [info] [<0.593.0>] 127.0.0.1 - - "GET /eee-test/_design/Meal" 404
[Fri, 13 Mar 2009 00:26:39 GMT] [info] [<0.594.0>] 127.0.0.1 - - "PUT /eee-test/_design/Meal" 201
I am unsure if the problem is caused by the attempt at accessing the Merb::DataMapperSessionStore or the initial Meal attempt.

No matter—I have it working well enough that I could make a go at outside-in development of my chain on Merb. So the question is: Sinatra or Merb?

No comments:

Post a Comment