Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Driver swapping on RSpec 2 #187

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/capybara/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ def use_default_driver
@current_driver = nil
end

##
#
# Yield a block using a specific driver
#
def using_driver(driver)
Capybara.current_driver = driver
yield
ensure
Capybara.use_default_driver
end

##
#
# The current Capybara::Session base on what is set as Capybara.app and Capybara.current_driver
Expand Down
12 changes: 12 additions & 0 deletions lib/capybara/rspec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if defined? RSpec::Core::Example
class RSpec::Core::Example

alias_method :__run_before_swinger, :run
private :__run_before_swinger

def run(*args)
Capybara.using_driver(metadata[:driver]) { __run_before_swinger(*args) }
end

end
end
26 changes: 26 additions & 0 deletions spec/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,32 @@
end
end

describe '#using_driver' do
before do
Capybara.current_driver.should_not == :selenium
end

it 'should set the driver using Capybara.current_driver=' do
driver = nil
Capybara.using_driver(:selenium) { driver = Capybara.current_driver }
driver.should == :selenium
end

it 'should reset the driver using Capybara.use_default_driver, even if an exception occurs' do
begin
Capybara.using_driver(:selenium) { raise "ohnoes!" }
rescue Exception
end
Capybara.current_driver.should == Capybara.default_driver
end

it 'should yield the passed block' do
called = false
Capybara.using_driver(:selenium) { called = true }
called.should == true
end
end

describe '#app' do
it "should be changeable" do
Capybara.app = "foobar"
Expand Down
21 changes: 21 additions & 0 deletions spec/rspec_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'spec_helper'
require 'capybara/dsl'
require 'capybara/rspec'

describe RSpec::Core::Example do

before do
@group = RSpec::Core::ExampleGroup.describe
end

it 'should call Capybara.using_driver' do
Capybara.should_receive(:using_driver).with(:selenium)
@group.example("does something", {:driver => :selenium}).run
end

it "does not show the original aliased method" do
methods = @group.example("without public aliased method").methods
methods.should_not include('__run_before_swinger')
end

end