Skip to content
Browse files

Support custom strategies for javascript scenarios.

Closes #184
  • Loading branch information...
1 parent 6d0eeb5 commit 7656a4528fe7c74b5eed15a3503fdf8931db6e3a @mattwynne mattwynne committed Nov 9, 2011
Showing with 33 additions and 36 deletions.
  1. +3 −1 lib/cucumber/rails/database.rb
  2. +30 −35 spec/cucumber/rails/database_spec.rb
View
4 lib/cucumber/rails/database.rb
@@ -4,13 +4,15 @@ module Database
CUSTOM_STRATEGY_INTERFACE = %w{ before_js before_non_js }
+ class InvalidStrategy < ArgumentError;end
+
class << self
def javascript_strategy=(strategy)
strategy_type =
case strategy
when Symbol
- map[strategy] || raise("The strategy '#{strategy}' is not understood. Please use one of #{map.keys.join(',')}")
+ map[strategy] || raise(InvalidStrategy, "The strategy '#{strategy}' is not understood. Please use one of #{map.keys.join(',')}")
when Class
strategy
end
View
65 spec/cucumber/rails/database_spec.rb
@@ -1,35 +1,26 @@
# -*- 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
+ let(:strategy) { stub(:before_js => nil, :before_non_js => nil) }
- def self.run!(name)
- @@callbacks[name].map(&:call)
- end
- end
+ it 'forwards events to the selected strategy' do
+ Cucumber::Rails::Database::TruncationStrategy.stub(:new => strategy)
+ Cucumber::Rails::Database.javascript_strategy = :truncation
- class Object
- def Before(name, &block)
- QuickCallback.add_callback(name, block)
- end
+ strategy.should_receive(:before_non_js).ordered
+ Cucumber::Rails::Database.before_non_js
+
+ strategy.should_receive(:before_js).ordered
+ Cucumber::Rails::Database.before_js
end
- it 'should accept custom JS DB strategies' do
- require 'lib/cucumber/rails/hooks/active_record'
+ it 'raises an error if you use a non-understood strategy' do
+ expect { Cucumber::Rails::Database.javascript_strategy = :invalid }.to raise_error(Cucumber::Rails::Database::InvalidStrategy)
+ end
+ describe 'using a custom strategy' do
class ValidStrategy
def before_js
# Anything
@@ -40,23 +31,27 @@ def before_non_js
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
+ class InvalidStrategy
+ end
- it 'should reject invalid JS DB strategies' do
- require 'lib/cucumber/rails/hooks/active_record'
+ it 'raises an error if the strategy doens\'t support the protocol' do
+ expect { Cucumber::Rails::Database.javascript_strategy = InvalidStrategy }.to raise_error(ArgumentError)
+ end
- class InvalidStrategy
+ it 'accepts a custom strategy with a valid interface' do
+ expect { Cucumber::Rails::Database.javascript_strategy = ValidStrategy }.not_to raise_error
end
- lambda { Cucumber::Rails::Database.javascript_strategy = InvalidStrategy }.should raise_error(ArgumentError)
+ it 'forwards events to a custom strategy' do
+ ValidStrategy.stub(:new => strategy)
+ Cucumber::Rails::Database.javascript_strategy = ValidStrategy
+
+ strategy.should_receive(:before_non_js).ordered
+ Cucumber::Rails::Database.before_non_js
+
+ strategy.should_receive(:before_js).ordered
+ Cucumber::Rails::Database.before_js
+ end
end
end

0 comments on commit 7656a45

Please sign in to comment.
Something went wrong with that request. Please try again.