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 cucumberAfter a bit of research, I realize that I am going to need to install mechanize to build my testing session:
(::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::)
(::) 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
cstrom@whitefall:~/repos/cuke_couch$ gem install mechanizeI then configure cucumber for mechanize testing by creating a
Successfully installed mechanize-1.0.0
1 gem installed
features/support/env.rb
file with:require 'webrat'(taken directly from the Cucumber documentation for mechanize)
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
I know from last year's chain that I'll need something like the following to create / tear down my test CouchDB database:
Before doFor now, I am not going to worry about that. Rather I am simply going to verify connectivity from Cucumber. I define the following in
# 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
features/test.feature
:Feature: Doing awesome stuff with CouchDBWhen I run that test, I get:
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
cstrom@whitefall:~/repos/cuke_couch$ cucumber features/test.featureSo far, so good. I define those steps in
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
features/humble_web_developer/test.rb
:When /^I GET a document$/ doJust to make sure that I put the code in the right place, I check that both steps are defined but pending:
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
cstrom@whitefall:~/repos/cuke_couch$ cucumber features/test.featureAll that is left now is to connect.
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
I un-pend the step definitions with:
When /^I GET a document$/ doI 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:
visit 'http://localhost:5984/seed/test'
end
Then /^I should have JSON$/ do
response_body.should contain('{')
end
cstrom@whitefall:~/repos/cuke_couch$ cucumber features/test.featureI'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:
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
cstrom@whitefall:~/repos/cuke_couch$ gem install rspecWith that, my simple connectivity scenario passes:
**************************************************
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
cstrom@whitefall:~/repos/cuke_couch$ cucumber features/test.featureNice! I am now confident that I can drive any CouchDB application to completion with Cucumber.
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
Day #75
No comments:
Post a Comment