Skip to content

Commit 2fb410b

Browse files
author
Sam Phippen
authored
Integrate with ActionDispatch::SystemTest (rspec#1813)
1 parent 5d93b79 commit 2fb410b

File tree

10 files changed

+170
-3
lines changed

10 files changed

+170
-3
lines changed

Gemfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
source "https://rubygems.org"
2+
RAILS_VERSION = ENV['RAILS_VERSION'] || (File.exist?(version_file) && File.read(version_file).chomp)
23

34
gemspec
45

@@ -37,9 +38,14 @@ if RUBY_VERSION < '2.0.0'
3738
gem 'mime-types', '< 3'
3839
end
3940

41+
4042
# Capybara versions that support RSpec 3 only support RUBY_VERSION >= 1.9.3
4143
if RUBY_VERSION >= '1.9.3'
42-
gem 'capybara', '~> 2.2.0', :require => false
44+
if /5(\.|-)1/ === RAILS_VERSION || "master" == RAILS_VERSION
45+
gem 'capybara', '~> 2.13', :require => false
46+
else
47+
gem 'capybara', '~> 2.2.0', :require => false
48+
end
4349
end
4450

4551
# Rack::Cache 1.3.0 requires Ruby >= 2.0.0

Gemfile-rails-dependencies

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,17 @@ when /master/
1313
gem 'i18n', :git => 'git://github.com/svenfuchs/i18n.git', :branch => 'master'
1414
gem 'sprockets', :git => 'git://github.com/rails/sprockets.git', :branch => 'master'
1515
gem 'sprockets-rails', :git => 'git://github.com/rails/sprockets-rails.git', :branch => 'master'
16+
if RUBY_VERSION >= "1.9.3"
17+
gem 'puma', :git => 'git://github.com/puma/puma', :branch => 'master'
18+
end
1619
when /stable$/
1720
gem_list = %w[rails railties actionmailer actionpack activerecord activesupport]
1821
gem_list << 'activejob' if version > '4-1-stable'
1922
gem_list << 'actionview' if version > '4-0-stable'
23+
if RUBY_VERSION >= "1.9.3"
24+
gem_list << 'puma' if version > '5-0-stable'
25+
end
26+
2027
gem_list.each do |rails_gem|
2128
gem rails_gem, :git => "git://github.com/rails/rails.git", :branch => version
2229
end
@@ -32,6 +39,10 @@ when nil, false, ""
3239
end
3340
else
3441
gem "rails", version
42+
43+
if version >= '5-1-stable' && RUBY_VERSION >= "1.9.3"
44+
gem "puma"
45+
end
3546
end
3647

3748
gem "i18n", '< 0.7.0' if RUBY_VERSION < '1.9.3'

Rakefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ end
2828

2929
Cucumber::Rake::Task.new(:cucumber) do |t|
3030
version = ENV.fetch("RAILS_VERSION", "~> 4.2.0")
31-
cucumber_flag = "--tags ~@rails_post_5"
3231
tags = []
32+
3333
if version.to_f >= 5.1
3434
tags << "~@rails_pre_5.1"
3535
end
@@ -38,8 +38,13 @@ Cucumber::Rake::Task.new(:cucumber) do |t|
3838
tags << "~@rails_pre_5"
3939
end
4040

41+
if version.to_f == 5.0
42+
tags << "~@system_test"
43+
end
44+
4145
if tags.empty?
4246
tags << "~@rails_post_5"
47+
tags << "~@system_test"
4348
end
4449

4550
cucumber_flag = tags.map { |tag| "--tag #{tag}" }

example_app_generator/generate_app.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
gsub_file "Gemfile", /.*web-console.*/, ''
2222
gsub_file "Gemfile", /.*debugger.*/, ''
2323
gsub_file "Gemfile", /.*byebug.*/, "gem 'byebug', '~> 9.0.6'"
24+
gsub_file "Gemfile", /.*puma.*/, ""
2425

2526
if Rails::VERSION::STRING >= '5.0.0'
2627
append_to_file('Gemfile', "gem 'rails-controller-testing', :git => 'https://github.com/rails/rails-controller-testing'\n")
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Feature: System spec
2+
3+
System specs are RSpec's wrapper around Rails' own
4+
[system tests](http://guides.rubyonrails.org/testing.html#system-testing).
5+
We encourage you to familiarse yourself with their documentation.
6+
7+
RSpec **does not** use your `ApplicationSystemTestCase` helper. Instead it uses
8+
the default `driven_by(:selenium)` from Rails. If you want to override this
9+
behaviour you can call `driven_by` manually in a test.
10+
11+
12+
@system_test
13+
Scenario: System specs
14+
Given a file named "spec/system/widget_system_spec.rb" with:
15+
"""ruby
16+
require "rails_helper"
17+
18+
RSpec.describe "Widget management", :type => :system do
19+
before do
20+
driven_by(:rack_test)
21+
end
22+
23+
it "enables me to create widgets" do
24+
visit "/widgets/new"
25+
26+
fill_in "Name", :with => "My Widget"
27+
click_button "Create Widget"
28+
29+
expect(page).to have_text("Widget was successfully created.")
30+
end
31+
end
32+
"""
33+
When I run `rspec spec/system/widget_system_spec.rb`
34+
Then the exit status should be 0
35+
And the output should contain "1 example, 0 failures"

lib/rspec/rails/configuration.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class Configuration
3333
:request => %w[spec (requests|integration|api)],
3434
:routing => %w[spec routing],
3535
:view => %w[spec views],
36-
:feature => %w[spec features]
36+
:feature => %w[spec features],
37+
:system => %w[spec system]
3738
}
3839

3940
# Sets up the different example group modules for the different spec types
@@ -48,6 +49,10 @@ def self.add_test_type_configurations(config)
4849
config.include RSpec::Rails::ViewExampleGroup, :type => :view
4950
config.include RSpec::Rails::FeatureExampleGroup, :type => :feature
5051
config.include RSpec::Rails::Matchers
52+
53+
if ActionPack::VERSION::STRING >= "5.1"
54+
config.include RSpec::Rails::SystemExampleGroup, :type => :system
55+
end
5156
end
5257

5358
# @private

lib/rspec/rails/example.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@
88
require 'rspec/rails/example/model_example_group'
99
require 'rspec/rails/example/job_example_group'
1010
require 'rspec/rails/example/feature_example_group'
11+
if ActionPack::VERSION::STRING >= "5.1"
12+
require 'rspec/rails/example/system_example_group'
13+
end
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
if ActionPack::VERSION::STRING >= "5.1"
2+
require 'action_dispatch/system_test_case'
3+
module RSpec
4+
module Rails
5+
# @api public
6+
# Container class for system tests
7+
module SystemExampleGroup
8+
extend ActiveSupport::Concern
9+
include RSpec::Rails::RailsExampleGroup
10+
include ActionDispatch::Integration::Runner
11+
include ActionDispatch::Assertions
12+
include RSpec::Rails::Matchers::RedirectTo
13+
include RSpec::Rails::Matchers::RenderTemplate
14+
include ActionController::TemplateAssertions
15+
16+
include ActionDispatch::IntegrationTest::Behavior
17+
18+
# @private
19+
module BlowAwayAfterTeardownHook
20+
# @private
21+
def after_teardown
22+
end
23+
end
24+
25+
original_after_teardown = ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown.instance_method(:after_teardown)
26+
27+
include ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown
28+
include ::ActionDispatch::SystemTesting::TestHelpers::ScreenshotHelper
29+
include BlowAwayAfterTeardownHook
30+
31+
# for the SystemTesting Screenshot situation
32+
def passed?
33+
RSpec.current_example.exception.nil?
34+
end
35+
36+
# @private
37+
def method_name
38+
[
39+
self.class.name.underscore,
40+
RSpec.current_example.description.underscore,
41+
rand(1000)
42+
].join("_").gsub(/[\/\.:, ]/, "_")
43+
end
44+
45+
# Delegates to `Rails.application`.
46+
def app
47+
::Rails.application
48+
end
49+
50+
included do
51+
attr_reader :driver
52+
53+
if ActionDispatch::SystemTesting::Server.respond_to?(:silence_puma=)
54+
ActionDispatch::SystemTesting::Server.silence_puma = true
55+
end
56+
57+
def initialize(*args, &blk)
58+
super(*args, &blk)
59+
@driver = nil
60+
end
61+
62+
def driven_by(*args, &blk)
63+
@driver = ::ActionDispatch::SystemTestCase.driven_by(*args, &blk).tap(&:use)
64+
end
65+
66+
before do
67+
# A user may have already set the driver, so only default if driver
68+
# is not set
69+
driven_by(:selenium) unless @driver
70+
@routes = ::Rails.application.routes
71+
end
72+
73+
after do
74+
orig_stdout = $stdout
75+
$stdout = StringIO.new
76+
begin
77+
original_after_teardown.bind(self).call
78+
ensure
79+
myio = $stdout
80+
RSpec.current_example.metadata[:extra_failure_lines] = myio.string
81+
$stdout = orig_stdout
82+
end
83+
end
84+
end
85+
end
86+
end
87+
end
88+
end

lib/rspec/rails/vendor/capybara.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
RSpec.configure do |c|
1818
if defined?(Capybara::DSL)
1919
c.include Capybara::DSL, :type => :feature
20+
if defined?(ActionPack) && ActionPack::VERSION::STRING >= "5.1"
21+
c.include Capybara::DSL, :type => :system
22+
end
2023
end
2124

2225
if defined?(Capybara::RSpecMatchers)
@@ -25,6 +28,7 @@
2528
c.include Capybara::RSpecMatchers, :type => :mailer
2629
c.include Capybara::RSpecMatchers, :type => :controller
2730
c.include Capybara::RSpecMatchers, :type => :feature
31+
c.include Capybara::RSpecMatchers, :type => :system
2832
end
2933

3034
unless defined?(Capybara::RSpecMatchers) || defined?(Capybara::DSL)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "spec_helper"
2+
module RSpec::Rails
3+
if defined?(SystemExampleGroup)
4+
RSpec.describe SystemExampleGroup do
5+
it_behaves_like "an rspec-rails example group mixin", :system,
6+
'./spec/system/', '.\\spec\\system\\'
7+
end
8+
end
9+
end

0 commit comments

Comments
 (0)