Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merging david changes to my development branch, also adding the trans…

…actional database on each scenario
  • Loading branch information...
commit 5e4c461695d831f95cab12ae167a7f26643ef559 2 parents 4012b70 + 9b0b3a0
Roman Gonzalez authored
View
2  .gitignore
@@ -1,2 +1,4 @@
pkg
doc/*
+\#*
+.#*
View
27 README
@@ -1,6 +1,29 @@
merb_cucumber
=============
-Merb + Cucumber integration
+Merb + Cucumber integration with Webrat support.
- merb-gen cucumber
+
+Installation
+------------
+
+If you want to use webrat, you need to install the fork at http://github.com/david/webrat.git
+
+
+First Time
+----------
+
+ cd your_merb_app
+ merb-gen cucumber
+
+For Webrat run this instead:
+
+ merb-gen cucumber --session-type webrat
+
+It will also install a simple login feature that should pass if you're using merb-auth.
+
+
+Usage
+-----
+
+ rake features
View
4 TODO
@@ -1,4 +1,2 @@
TODO:
-Handle ORM transactions before and after each scenario
-Detect the ORM
-Handle World, according to the ORM
+Fix bugs in the RequestMatchers from merb-core
View
23 lib/generators/cucumber.rb
@@ -1,24 +1,41 @@
module Merb::Generators
+
class CucumberGenerator < Generator
desc <<-DESC
Generates setup code for cucumber feature framework
DESC
-
def self.source_root
File.join(File.dirname(__FILE__), 'cucumber', 'templates')
end
+ option :orm, :desc => 'Object-Relation Mapper to use (one of: none, activerecord, datamapper, sequel)'
+
+ option :session_type, :default => :simple, :desc => 'Session type to use (one of: simple, webrat)'
+
template(:env) { |t| t.source = t.destination = "features/env.rb" }
- # Webrat is not well supported (yet)
- #template(:common_webrat) { |t| t.source = t.destination = "features/steps/common_webrat.rb" }
+
+ template(:example_feature, :session_type => :webrat) do |t|
+ t.source = t.destination = "features/login.feature"
+ end
+
+ template(:example_feature_steps, :session_type => :webrat) do |t|
+ t.source = t.destination = "features/steps/login_steps.rb"
+ end
+
+ template(:common_webrat, :session_type => :webrat) do |t|
+ t.source = t.destination = "features/steps/common_webrat.rb"
+ end
+
template(:cucumber, :after => :chmod) { |t| t.source = t.destination = "bin/cucumber" }
def chmod(action)
File.chmod(0755, action.destination)
end
+
end
add :cucumber, CucumberGenerator
+
end
View
11 lib/generators/cucumber/templates/features/env.rb
@@ -9,8 +9,15 @@
require "merb-core"
require "spec"
+<% if session_type == :webrat -%>
+require "webrat"
+<% end -%>
+require "merb_cucumber/world/<%= session_type %>"
+<% if Merb.orm == :datamapper -%>
+require "merb_cucumber/helpers/datamapper"
+<% elsif Merb.orm == :activerecord -%>
+require "merb_cucumber/helpers/activerecord"
+<% end -%>
Merb.start_environment(:testing => true, :adapter => 'runner', :environment => ENV['MERB_ENV'] || 'test')
-
-require 'merb_cucumber/feature'
View
12 lib/generators/cucumber/templates/features/login.feature
@@ -0,0 +1,12 @@
+Feature: Login
+ To ensure the safety of the application
+ A regular user of the system
+ Must authenticate before using the app
+
+ Scenario: Failed Login
+ Given I am not authenticated
+ When I go to /login
+ And I fill in "login" with "i_dont_exist"
+ And I fill in "password" with "and_i_dont_have_a_password"
+ And I press "Log In"
+ Then I should see "Login or Password were incorrect"
View
4 lib/generators/cucumber/templates/features/steps/common_webrat.rb
@@ -1,6 +1,10 @@
# Commonly used webrat steps
# http://github.com/brynary/webrat
+When /^I go to (.*)$/ do |path|
+ visits path
+end
+
When /^I press "(.*)"$/ do |button|
clicks_button(button)
end
View
3  lib/generators/cucumber/templates/features/steps/login_steps.rb
@@ -0,0 +1,3 @@
+Given /^I am not authenticated$/ do
+ # yay!
+end
View
8 lib/merb_cucumber.rb
@@ -1,11 +1,3 @@
if defined?(Merb::Plugins)
Merb::Plugins.add_rakefiles "merb_cucumber" / "merbtasks"
-
- # this should be called only on features/env.rb
- #if Merb.environment == "test"
- # Merb::BootLoader.after_app_loads do
- # require File.join(File.dirname(__FILE__) / "merb_cucumber" / "feature")
- # end
- #end
-
end
View
47 lib/merb_cucumber/feature.rb → lib/merb_cucumber/helpers/activerecord.rb
@@ -1,36 +1,8 @@
-$main = self
+$main = self # This must be included in the env.rb file!
module Merb
module Test
-
- class World
- include Merb::Test::ControllerHelper
- include Merb::Test::RouteHelper
- include Merb::Test::ViewHelper
-
- module DataMapper
-
- def self.included(base)
-
- $main.Before do
- # Getting all the adapters configured in DataMapper
- puts "\x1B[44mStarting a transaction for each repository\x1B[0m"
- @__transactions__ = ::DataMapper::Repository.adapters.values.map do |adapter|
- transaction = ::DataMapper::Transaction.new(adapter)
- transaction.begin
- transaction
- end
- end
-
- $main.After do
- puts "\x1B[41mDoing a Rollback of the transactions\x1B[0m"
- @__transactions__.each { |transaction| transaction.rollback }
- end
-
- end
-
- end
-
+ module Helpers
module ActiveRecord
def self.included(base)
@@ -58,19 +30,12 @@ def self.included(base)
end
end
-
+ end
- if Merb.orm == :datamapper
- include DataMapper
- elsif Merb.orm == :activerecord
- include ActiveRecord
+ module World
+ module Base
+ include Helpers::ActiveRecord
end
-
end
-
end
end
-
-World do
- Merb::Test::World.new
-end
View
36 lib/merb_cucumber/helpers/datamapper.rb
@@ -0,0 +1,36 @@
+$main = self # This must be included in the env.rb file!
+
+module Merb
+ module Test
+ module Helpers
+ module DataMapper
+
+ def self.included(base)
+
+ $main.Before do
+ # Getting all the adapters configured in DataMapper
+ puts "\x1B[44mStarting a transaction for each repository\x1B[0m"
+ @__transactions__ = ::DataMapper::Repository.adapters.values.map do |adapter|
+ transaction = ::DataMapper::Transaction.new(adapter)
+ transaction.begin
+ transaction
+ end
+ end
+
+ $main.After do
+ puts "\x1B[41mDoing a Rollback of the transactions\x1B[0m"
+ @__transactions__.each { |transaction| transaction.rollback }
+ end
+
+ end
+
+ end
+ end
+
+ module World
+ module Base
+ include Helpers::DataMapper
+ end
+ end
+ end
+end
View
10 lib/merb_cucumber/merbtasks.rb
@@ -6,6 +6,12 @@
t.cucumber_opts = "--format pretty"
end
-if defined?(DataMapper)
- task :features => 'db:automigrate'
+namespace :merb_cucumber do
+ task :test_env do
+ Merb.start_environment(:environment => "test", :adapter => 'runner')
+ end
+end
+
+if Merb.orm == :datamapper
+ task :features => ['merb_cucumber:test_env', 'db:automigrate']
end
View
11 lib/merb_cucumber/world/base.rb
@@ -0,0 +1,11 @@
+module Merb
+ module Test
+ module World
+ module Base
+ include Merb::Test::ControllerHelper
+ include Merb::Test::RouteHelper
+ include Merb::Test::ViewHelper
+ end
+ end
+ end
+end
View
15 lib/merb_cucumber/world/simple.rb
@@ -0,0 +1,15 @@
+require File.join(File.dirname(__FILE__), 'base')
+
+module Merb
+ module Test
+ module World
+ class Simple
+ include Base
+ end
+ end
+ end
+end
+
+World do
+ Merb::Test::World::Simple.new
+end
View
44 lib/merb_cucumber/world/webrat.rb
@@ -0,0 +1,44 @@
+require 'webrat/rack/rack_session'
+require File.join(File.dirname(__FILE__), 'base')
+
+module Merb
+ module Test
+ module World
+ class Webrat < Webrat::RackSession
+ include Base
+
+ attr_reader :response
+
+ %w(get head post put delete).each do |verb|
+ define_method(verb) do |*args| # (path, data, headers = nil)
+ path, data, headers = *args
+ all = (headers || {})
+ all.merge!(:method => "#{verb.upcase}") unless all[:method] || all["REQUEST_METHOD"]
+
+ unless data.empty?
+ if verb == "post"
+ all.merge!(:body_params => data)
+ elsif verb == "get"
+ all.merge!(:params => data)
+ end
+ end
+
+ @response = request(path, all)
+
+ class << @response
+ def body
+ super.to_s
+ end
+ end
+
+ @response
+ end
+ end
+ end
+ end
+ end
+end
+
+World do
+ Merb::Test::World::Webrat.new
+end
Please sign in to comment.
Something went wrong with that request. Please try again.