Wednesday, March 11, 2009

Cucumber, Merb and CouchDB - Getting Closer

‹prev | My Chain | next›

Having gotten Sinatra & CouchDB playing nicely (including running cucumber tests), I would like to do the same with Merb.

Following along with http://wiki.github.com/aslakhellesoy/cucumber/merb, I will use the jsmestad-merb_cucumber gem (better webrat support). Install the gem:
strom@jaynestown:~$ gem install david-merb_cucumber
WARNING: Installing to ~/.gem since /usr/lib/ruby/gems/1.8 and
/usr/bin aren't both writable.
Successfully installed david-merb_cucumber-0.5.1.2
1 gem installed
And run the cucumber generator:
cstrom@jaynestown:~/repos/eee-merb$ merb-gen cucumber
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] bin/cucumber
[ADDED] cucumber.yml
So far so good, now let's drop our drop-dead simple browse_meal.feature into the features directory:
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"
A simple rake features will yield the steps that need to be implemented. Or not:
strom@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
rake aborted!
Unknown property 'views'

(See full trace by running task with --trace)
Hunh? Well, running the --trace option produces
cstrom@jaynestown:~/repos/eee-merb$ rake features --trace
(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
** Invoke features (first_time)
** Invoke merb_cucumber:test_env (first_time)
** Execute merb_cucumber:test_env
** Invoke db:automigrate (first_time)
** Invoke merb_env (first_time)
** Execute merb_env
** Execute db:automigrate
rake aborted!
Unknown property 'views'
/home/cstrom/.gem/ruby/1.8/gems/dm-core-0.9.10/lib/dm-core/model.rb:479:in `method_missing'
/home/cstrom/.gem/ruby/1.8/gems/dm-couchdb-adapter-0.9.10/lib/couchdb_adapter.rb:384:in `create_model_storage'
/home/cstrom/.gem/ruby/1.8/gems/dm-core-0.9.10/lib/dm-core/auto_migrations.rb:88:in `auto_migrate_up!'
/home/cstrom/.gem/ruby/1.8/gems/dm-core-0.9.10/lib/dm-core/model.rb:113:in `repository'
/home/cstrom/.gem/ruby/1.8/gems/dm-core-0.9.10/lib/dm-core.rb:181:in `repository'
/home/cstrom/.gem/ruby/1.8/gems/dm-core-0.9.10/lib/dm-core/repository.rb:44:in `scope'
/home/cstrom/.gem/ruby/1.8/gems/dm-core-0.9.10/lib/dm-core.rb:181:in `repository'
/home/cstrom/.gem/ruby/1.8/gems/dm-core-0.9.10/lib/dm-core/model.rb:113:in `repository'
/home/cstrom/.gem/ruby/1.8/gems/dm-core-0.9.10/lib/dm-core/auto_migrations.rb:87:in `auto_migrate_up!'
/home/cstrom/.gem/ruby/1.8/gems/dm-core-0.9.10/lib/dm-core/auto_migrations.rb:39:in `auto_migrate_up'
/home/cstrom/.gem/ruby/1.8/gems/dm-core-0.9.10/lib/dm-core/auto_migrations.rb:38:in `each'
/home/cstrom/.gem/ruby/1.8/gems/dm-core-0.9.10/lib/dm-core/auto_migrations.rb:38:in `auto_migrate_up'
/home/cstrom/.gem/ruby/1.8/gems/dm-core-0.9.10/lib/dm-core/auto_migrations.rb:13:in `auto_migrate'
/home/cstrom/.gem/ruby/1.8/gems/merb_datamapper-1.0.9/lib/merb_datamapper/merbtasks.rb:19
/usr/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:617:in `call'
...
A cursory examination of the dm-couchdb-adapter gem's DataMapper::CouchResource reveals that it more certainly does define views, so what's up?

When weird things happen with CouchDB, I am learing to check the log files. In this case, the couch.log file contains:
[Thu, 12 Mar 2009 01:30:30 GMT] [info] [<0.183.0>] HTTP Error (code 404): not_found
[Thu, 12 Mar 2009 01:30:30 GMT] [info] [<0.183.0>] 127.0.0.1 - - "GET /eee-test/_design/User" 404
[Thu, 12 Mar 2009 01:30:30 GMT] [info] [<0.184.0>] HTTP Error (code 404): not_found
[Thu, 12 Mar 2009 01:30:30 GMT] [info] [<0.184.0>] 127.0.0.1 - - "GET /eee-test/_design/Merb::DataMapperSessionStore" 404
[Thu, 12 Mar 2009 01:30:30 GMT] [info] [<0.187.0>] HTTP Error (code 404): not_found
[Thu, 12 Mar 2009 01:30:30 GMT] [info] [<0.187.0>] 127.0.0.1 - - "GET /eee-test/_design/Meal" 404
[Thu, 12 Mar 2009 01:30:30 GMT] [info] [<0.190.0>] HTTP Error (code 404): not_found
[Thu, 12 Mar 2009 01:30:30 GMT] [info] [<0.190.0>] 127.0.0.1 - - "PUT /eee-test/_design/Meal" 404
Urg. It is not even creating the DB in the CouchDB server. I really ought to investigate how the CouchDB adapter implements the underlying db:automigrate, but the easiest thing to do is to remove the dependency from cucumber.rake (dependencies = ['merb_cucumber:test_env'] #, 'db:automigrate']):
require 'cucumber/rake/task'

cucumber_options = lambda do |t|
# if you want to pass some custom options to cucumber, pass them here
end

Cucumber::Rake::Task.new(:features, &cucumber_options)
Cucumber::Rake::FeatureTask.new(:feature, &cucumber_options)
namespace :merb_cucumber do
task :test_env do
Merb.start_environment(:environment => "test", :adapter => 'runner')
end
end


dependencies = ['merb_cucumber:test_env'] #, 'db:automigrate']
task :features => dependencies
task :feature => dependencies
That does the trick, at least in the sense that we get the empty step definitions (though the test DB is still not being created):
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/browse_meals.feature:7
When I view the meal permalink # features/browse_meals.feature:8
Then the title should include "Breakfast Elves" # features/browse_meals.feature:9


1 scenario
3 steps pending (3 with no step definition)

You can use these snippets to implement pending steps which have no step definition:

Given /^a "Breakfast Elves" meal$/ do
end

When /^I view the meal permalink$/ do
end

Then /^the title should include "Breakfast Elves"$/ do
end



Finished in 0.002987 seconds

0 examples, 0 failures

And Fail...

Implementing the steps and the Before / After blocks in features/support/env.rb as I did for Sinatra the other night gets me close to a working Cucumber run, but I end up with:
cstrom@jaynestown:~/repos/eee-merb$ cucumber features -n
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"
undefined local variable or method `response' for # (NameError)
./features/steps/browse_meals.rb:17:in `Then /^the title should include "(.+)"$/'
features/browse_meals.feature:9:in `Then the title should include "Breakfast Elves"'
This looks to be the same error as the one noted in this thread; however initial attempts at resolving the problem as suggested there were not successful.

I am getting frustrted with Merb, but will likely give it another try tomorrow night.

No comments:

Post a Comment