Friday, April 16, 2010

CouchDB Cucumber

‹prev | My Chain | next›

Tonight I'd like to see if I can get basic Cucumber testing working with a pure CouchDB application (like couchapp or node.couchapp.js).

First up, I install cucumber and webrat:
cstrom@whitefall:~/repos/cuke_couch$ gem install cucumber

(::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::)

(::) U P G R A D I N G (::)

Thank you for installing cucumber-0.6.4.
Please be sure to read http://wiki.github.com/aslakhellesoy/cucumber/upgrading
for important information about this release. Happy cuking!

(::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::)

Successfully installed term-ansicolor-1.0.5
Successfully installed polyglot-0.3.1
Successfully installed treetop-1.4.5
Successfully installed builder-2.1.2
Successfully installed diff-lcs-1.1.2
Successfully installed cucumber-0.6.4
6 gems installed
cstrom@whitefall:~/repos/cuke_couch$ gem install webrat
Building native extensions. This could take a while...
Successfully installed nokogiri-1.4.1
Successfully installed rack-1.1.0
Successfully installed rack-test-0.5.3
Successfully installed webrat-0.7.0
4 gems installed
After a bit of research, I realize that I am going to need to install mechanize to build my testing session:
cstrom@whitefall:~/repos/cuke_couch$ gem install mechanize
Successfully installed mechanize-1.0.0
1 gem installed
I then configure cucumber for mechanize testing by creating a features/support/env.rb file with:
require 'webrat'

Webrat.configure do |config|
config.mode = :mechanize
end

class MechanizeWorld < Webrat::MechanizeAdapter
include Webrat::Matchers
include Webrat::Methods
# no idea why we need this but without it response_code is not always recognized
Webrat::Methods.delegate_to_session :response_code, :response_body
end

World do
MechanizeWorld.new
end
(taken directly from the Cucumber documentation for mechanize)

I know from last year's chain that I'll need something like the following to create / tear down my test CouchDB database:
Before do
# Create the DB
RestClient.put @@db, { }

# Upload the design documents with a super-easy gem :)
CouchDocs.upload_dir(@@db, 'couch')
end

After do
# Delete the DB
RestClient.delete @@db
end
For now, I am not going to worry about that. Rather I am simply going to verify connectivity from Cucumber. I define the following in features/test.feature:
Feature: Doing awesome stuff with CouchDB

So that I can do amaizing things
As a humble web developer
I want to connect to CouchDB

Scenario: GET a document
When I GET a document
Then I should have JSON
When I run that test, I get:
cstrom@whitefall:~/repos/cuke_couch$ cucumber features/test.feature 
Feature: Doing awesome stuff with CouchDB

So that I can do amaizing things
As a humble web developer
I want to connect to CouchDB

Scenario: GET a document # features/test.feature:7
When I GET a document # features/test.feature:8
Then I should have JSON # features/test.feature:9

1 scenario (1 undefined)
2 steps (2 undefined)
0m0.011s

You can implement step definitions for undefined steps with these snippets:

When /^I GET a document$/ do
pending # express the regexp above with the code you wish you had
end

Then /^I should have JSON$/ do
pending # express the regexp above with the code you wish you had
end
So far, so good. I define those steps in features/humble_web_developer/test.rb:
When /^I GET a document$/ do
pending # express the regexp above with the code you wish you had
end

Then /^I should have JSON$/ do
pending # express the regexp above with the code you wish you had
end
Just to make sure that I put the code in the right place, I check that both steps are defined but pending:
cstrom@whitefall:~/repos/cuke_couch$ cucumber features/test.feature 
Feature: Doing awesome stuff with CouchDB

So that I can do amaizing things
As a humble web developer
I want to connect to CouchDB

Scenario: GET a document # features/test.feature:7
When I GET a document # features/step_definitions/humble_web_developer/test.rb:1
TODO (Cucumber::Pending)
./features/step_definitions/humble_web_developer/test.rb:2:in `/^I GET a document$/'
features/test.feature:8:in `When I GET a document'
Then I should have JSON # features/step_definitions/humble_web_developer/test.rb:5

1 scenario (1 pending)
2 steps (1 skipped, 1 pending)
0m0.008s
All that is left now is to connect.

I un-pend the step definitions with:
When /^I GET a document$/ do
visit 'http://localhost:5984/seed/test'
end

Then /^I should have JSON$/ do
response_body.should contain('{')
end
I real life I would certainly make that second step definition a little more robust, but for now I plow ahead. Running the steps, I find:
cstrom@whitefall:~/repos/cuke_couch$ cucumber features/test.feature 
Feature: Doing awesome stuff with CouchDB

So that I can do amaizing things
As a humble web developer
I want to connect to CouchDB

Scenario: GET a document # features/test.feature:7
!!!!! DEPRECATION NOTICE !!!!!
The WWW constant is deprecated, please switch to the new top-level Mechanize
constant. WWW will be removed in Mechanize version 2.0

You've referenced the WWW constant from /home/cstrom/.rvm/gems/ruby-1.8.7-p249/gems/webrat-0.7.0/lib/webrat/adapters/mechanize.rb:43:in `mechanize', please
switch the "WWW" to "Mechanize". Thanks!

Sincerely,

Pew Pew Pew

When I GET a document # features/step_definitions/humble_web_developer/test.rb:1
Then I should have JSON # features/step_definitions/humble_web_developer/test.rb:5
undefined method `should' for #<String:0xb6bc5cf8> (NoMethodError)
./features/step_definitions/humble_web_developer/test.rb:6:in `/^I should have JSON$/'
features/test.feature:9:in `Then I should have JSON'


Failing Scenarios:
cucumber features/test.feature:7 # Scenario: GET a document

1 scenario (1 failed)
2 steps (1 failed, 1 passed)
0m0.058s
I'm gonna ignore that deprecation warning entirely, but I need to resolve that "undefined method `should'" error. That is easy enough—I simply need to install RSpec:
cstrom@whitefall:~/repos/cuke_couch$ gem install rspec
**************************************************

Thank you for installing rspec-1.3.0

Please be sure to read History.rdoc and Upgrade.rdoc
for useful information about this release.

**************************************************
Successfully installed rspec-1.3.0
1 gem installed
With that, my simple connectivity scenario passes:
cstrom@whitefall:~/repos/cuke_couch$ cucumber features/test.feature 
Feature: Doing awesome stuff with CouchDB

So that I can do amaizing things
As a humble web developer
I want to connect to CouchDB

Scenario: GET a document # features/test.feature:7
When I GET a document # features/step_definitions/humble_web_developer/test.rb:1
Then I should have JSON # features/step_definitions/humble_web_developer/test.rb:5

1 scenario (1 passed)
2 steps (2 passed)
0m0.042s
Nice! I am now confident that I can drive any CouchDB application to completion with Cucumber.

Day #75

No comments:

Post a Comment