Skip to content

Commit 850f450

Browse files
committed
Fix an issue where driven_by(:selenium) is always called
Because the before process defined in SystemExampleGroup is executed before the use defined before process, driven_by(:selenium) is always executed. This commit fixes to check the driver configuration at the end of before hooks.
1 parent 33ab374 commit 850f450

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

lib/rspec/rails/example/system_example_group.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,19 @@ def app
7878
def initialize(*args, &blk)
7979
super(*args, &blk)
8080
@driver = nil
81+
82+
self.class.before do
83+
# A user may have already set the driver, so only default if driver
84+
# is not set
85+
driven_by(:selenium) unless @driver
86+
end
8187
end
8288

8389
def driven_by(*args, &blk)
8490
@driver = ::ActionDispatch::SystemTestCase.driven_by(*args, &blk).tap(&:use)
8591
end
8692

8793
before do
88-
# A user may have already set the driver, so only default if driver
89-
# is not set
90-
driven_by(:selenium) unless @driver
9194
@routes = ::Rails.application.routes
9295
end
9396

spec/rspec/rails/example/system_example_group_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,47 @@ module RSpec::Rails
1818
end
1919
end
2020
end
21+
22+
describe '#driver' do
23+
it 'uses :selenium driver by default' do
24+
group = RSpec::Core::ExampleGroup.describe do
25+
include SystemExampleGroup
26+
end
27+
example = group.new
28+
group.hooks.run(:before, :example, example)
29+
30+
expect(Capybara.current_driver).to eq :selenium
31+
end
32+
33+
it 'sets :rack_test driver using by before_action' do
34+
group = RSpec::Core::ExampleGroup.describe do
35+
include SystemExampleGroup
36+
37+
before do
38+
driven_by(:rack_test)
39+
end
40+
end
41+
example = group.new
42+
group.hooks.run(:before, :example, example)
43+
44+
expect(Capybara.current_driver).to eq :rack_test
45+
end
46+
47+
it 'calls :driven_by method only once' do
48+
group = RSpec::Core::ExampleGroup.describe do
49+
include SystemExampleGroup
50+
51+
before do
52+
driven_by(:rack_test)
53+
end
54+
end
55+
example = group.new
56+
allow(example).to receive(:driven_by).and_call_original
57+
group.hooks.run(:before, :example, example)
58+
59+
expect(example).to have_received(:driven_by).once
60+
end
61+
end
2162
end
2263
end
2364
end

0 commit comments

Comments
 (0)