Skip to content

Commit

Permalink
Adding support for custom JS DB strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Pabst authored and Michael Pabst committed Nov 7, 2011
1 parent 192768e commit 6d0eeb5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/cucumber/rails/database.rb
@@ -1,11 +1,23 @@
module Cucumber
module Rails
module Database

CUSTOM_STRATEGY_INTERFACE = %w{ before_js before_non_js }

class << self

def javascript_strategy=(strategy)
strategy_type = map[strategy] || raise("The strategy '#{strategy}' is not understood. Please use one of #{map.keys.join(',')}")
strategy_type =
case strategy
when Symbol
map[strategy] || raise("The strategy '#{strategy}' is not understood. Please use one of #{map.keys.join(',')}")
when Class
strategy
end

@strategy = strategy_type.new

validate_interface!
end

def before_js
Expand All @@ -26,6 +38,12 @@ def map
}
end

def validate_interface!
unless CUSTOM_STRATEGY_INTERFACE.all? {|m| @strategy.respond_to?(m) }
raise(ArgumentError, "Strategy must respond to all of: #{CUSTOM_STRATEGY_INTERFACE.map{|method| "##{method}" } * ' '} !")
end
end

end

class SharedConnectionStrategy
Expand Down
62 changes: 62 additions & 0 deletions spec/cucumber/rails/database_spec.rb
@@ -0,0 +1,62 @@
# -*- encoding: utf-8 -*-

require 'spec_helper'
require 'cucumber'

require 'rails'
require 'active_record'
require 'cucumber/rails/database'


describe Cucumber::Rails::Database do

class QuickCallback
@@callbacks = Hash.new {|h,k| h[k] = []}
def self.add_callback(name, block)
@@callbacks[name] << block
end

def self.run!(name)
@@callbacks[name].map(&:call)
end
end

class Object
def Before(name, &block)
QuickCallback.add_callback(name, block)
end
end

it 'should accept custom JS DB strategies' do
require 'lib/cucumber/rails/hooks/active_record'

class ValidStrategy
def before_js
# Anything
end

def before_non_js
# Likewise
end
end

Cucumber::Rails::Database.javascript_strategy = ValidStrategy
# Fun Ruby fact (@frf): foo=(a) will ALWAYS return a (unless you do send(:foo=, a))
strategy = Cucumber::Rails::Database.instance_variable_get(:@strategy)
strategy.should_receive(:before_js).once
QuickCallback.run!('@javascript')

strategy.should_receive(:before_non_js).once
QuickCallback.run!('~@javascript')
end

it 'should reject invalid JS DB strategies' do
require 'lib/cucumber/rails/hooks/active_record'

class InvalidStrategy
end

lambda { Cucumber::Rails::Database.javascript_strategy = InvalidStrategy }.should raise_error(ArgumentError)
end

end

0 comments on commit 6d0eeb5

Please sign in to comment.