Skip to content

Commit

Permalink
Integrate with ActionDispatch::SystemTest (#1813)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Phippen authored Aug 26, 2017
1 parent 5d93b79 commit 2fb410b
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 3 deletions.
8 changes: 7 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
source "https://rubygems.org"
RAILS_VERSION = ENV['RAILS_VERSION'] || (File.exist?(version_file) && File.read(version_file).chomp)

gemspec

Expand Down Expand Up @@ -37,9 +38,14 @@ if RUBY_VERSION < '2.0.0'
gem 'mime-types', '< 3'
end


# Capybara versions that support RSpec 3 only support RUBY_VERSION >= 1.9.3
if RUBY_VERSION >= '1.9.3'
gem 'capybara', '~> 2.2.0', :require => false
if /5(\.|-)1/ === RAILS_VERSION || "master" == RAILS_VERSION
gem 'capybara', '~> 2.13', :require => false
else
gem 'capybara', '~> 2.2.0', :require => false
end
end

# Rack::Cache 1.3.0 requires Ruby >= 2.0.0
Expand Down
11 changes: 11 additions & 0 deletions Gemfile-rails-dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ when /master/
gem 'i18n', :git => 'git://github.com/svenfuchs/i18n.git', :branch => 'master'
gem 'sprockets', :git => 'git://github.com/rails/sprockets.git', :branch => 'master'
gem 'sprockets-rails', :git => 'git://github.com/rails/sprockets-rails.git', :branch => 'master'
if RUBY_VERSION >= "1.9.3"
gem 'puma', :git => 'git://github.com/puma/puma', :branch => 'master'
end
when /stable$/
gem_list = %w[rails railties actionmailer actionpack activerecord activesupport]
gem_list << 'activejob' if version > '4-1-stable'
gem_list << 'actionview' if version > '4-0-stable'
if RUBY_VERSION >= "1.9.3"
gem_list << 'puma' if version > '5-0-stable'
end

gem_list.each do |rails_gem|
gem rails_gem, :git => "git://github.com/rails/rails.git", :branch => version
end
Expand All @@ -32,6 +39,10 @@ when nil, false, ""
end
else
gem "rails", version

if version >= '5-1-stable' && RUBY_VERSION >= "1.9.3"
gem "puma"
end
end

gem "i18n", '< 0.7.0' if RUBY_VERSION < '1.9.3'
Expand Down
7 changes: 6 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ end

Cucumber::Rake::Task.new(:cucumber) do |t|
version = ENV.fetch("RAILS_VERSION", "~> 4.2.0")
cucumber_flag = "--tags ~@rails_post_5"
tags = []

if version.to_f >= 5.1
tags << "~@rails_pre_5.1"
end
Expand All @@ -38,8 +38,13 @@ Cucumber::Rake::Task.new(:cucumber) do |t|
tags << "~@rails_pre_5"
end

if version.to_f == 5.0
tags << "~@system_test"
end

if tags.empty?
tags << "~@rails_post_5"
tags << "~@system_test"
end

cucumber_flag = tags.map { |tag| "--tag #{tag}" }
Expand Down
1 change: 1 addition & 0 deletions example_app_generator/generate_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
gsub_file "Gemfile", /.*web-console.*/, ''
gsub_file "Gemfile", /.*debugger.*/, ''
gsub_file "Gemfile", /.*byebug.*/, "gem 'byebug', '~> 9.0.6'"
gsub_file "Gemfile", /.*puma.*/, ""

if Rails::VERSION::STRING >= '5.0.0'
append_to_file('Gemfile', "gem 'rails-controller-testing', :git => 'https://github.com/rails/rails-controller-testing'\n")
Expand Down
35 changes: 35 additions & 0 deletions features/system_specs/system_specs.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Feature: System spec

System specs are RSpec's wrapper around Rails' own
[system tests](http://guides.rubyonrails.org/testing.html#system-testing).
We encourage you to familiarse yourself with their documentation.

RSpec **does not** use your `ApplicationSystemTestCase` helper. Instead it uses
the default `driven_by(:selenium)` from Rails. If you want to override this
behaviour you can call `driven_by` manually in a test.


@system_test
Scenario: System specs
Given a file named "spec/system/widget_system_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.describe "Widget management", :type => :system do
before do
driven_by(:rack_test)
end
it "enables me to create widgets" do
visit "/widgets/new"
fill_in "Name", :with => "My Widget"
click_button "Create Widget"
expect(page).to have_text("Widget was successfully created.")
end
end
"""
When I run `rspec spec/system/widget_system_spec.rb`
Then the exit status should be 0
And the output should contain "1 example, 0 failures"
7 changes: 6 additions & 1 deletion lib/rspec/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class Configuration
:request => %w[spec (requests|integration|api)],
:routing => %w[spec routing],
:view => %w[spec views],
:feature => %w[spec features]
:feature => %w[spec features],
:system => %w[spec system]
}

# Sets up the different example group modules for the different spec types
Expand All @@ -48,6 +49,10 @@ def self.add_test_type_configurations(config)
config.include RSpec::Rails::ViewExampleGroup, :type => :view
config.include RSpec::Rails::FeatureExampleGroup, :type => :feature
config.include RSpec::Rails::Matchers

if ActionPack::VERSION::STRING >= "5.1"
config.include RSpec::Rails::SystemExampleGroup, :type => :system
end
end

# @private
Expand Down
3 changes: 3 additions & 0 deletions lib/rspec/rails/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
require 'rspec/rails/example/model_example_group'
require 'rspec/rails/example/job_example_group'
require 'rspec/rails/example/feature_example_group'
if ActionPack::VERSION::STRING >= "5.1"
require 'rspec/rails/example/system_example_group'
end
88 changes: 88 additions & 0 deletions lib/rspec/rails/example/system_example_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
if ActionPack::VERSION::STRING >= "5.1"
require 'action_dispatch/system_test_case'
module RSpec
module Rails
# @api public
# Container class for system tests
module SystemExampleGroup
extend ActiveSupport::Concern
include RSpec::Rails::RailsExampleGroup
include ActionDispatch::Integration::Runner
include ActionDispatch::Assertions
include RSpec::Rails::Matchers::RedirectTo
include RSpec::Rails::Matchers::RenderTemplate
include ActionController::TemplateAssertions

include ActionDispatch::IntegrationTest::Behavior

# @private
module BlowAwayAfterTeardownHook
# @private
def after_teardown
end
end

original_after_teardown = ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown.instance_method(:after_teardown)

include ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown
include ::ActionDispatch::SystemTesting::TestHelpers::ScreenshotHelper
include BlowAwayAfterTeardownHook

# for the SystemTesting Screenshot situation
def passed?
RSpec.current_example.exception.nil?
end

# @private
def method_name
[
self.class.name.underscore,
RSpec.current_example.description.underscore,
rand(1000)
].join("_").gsub(/[\/\.:, ]/, "_")
end

# Delegates to `Rails.application`.
def app
::Rails.application
end

included do
attr_reader :driver

if ActionDispatch::SystemTesting::Server.respond_to?(:silence_puma=)
ActionDispatch::SystemTesting::Server.silence_puma = true
end

def initialize(*args, &blk)
super(*args, &blk)
@driver = nil
end

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

before do
# A user may have already set the driver, so only default if driver
# is not set
driven_by(:selenium) unless @driver
@routes = ::Rails.application.routes
end

after do
orig_stdout = $stdout
$stdout = StringIO.new
begin
original_after_teardown.bind(self).call
ensure
myio = $stdout
RSpec.current_example.metadata[:extra_failure_lines] = myio.string
$stdout = orig_stdout
end
end
end
end
end
end
end
4 changes: 4 additions & 0 deletions lib/rspec/rails/vendor/capybara.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
RSpec.configure do |c|
if defined?(Capybara::DSL)
c.include Capybara::DSL, :type => :feature
if defined?(ActionPack) && ActionPack::VERSION::STRING >= "5.1"
c.include Capybara::DSL, :type => :system
end
end

if defined?(Capybara::RSpecMatchers)
Expand All @@ -25,6 +28,7 @@
c.include Capybara::RSpecMatchers, :type => :mailer
c.include Capybara::RSpecMatchers, :type => :controller
c.include Capybara::RSpecMatchers, :type => :feature
c.include Capybara::RSpecMatchers, :type => :system
end

unless defined?(Capybara::RSpecMatchers) || defined?(Capybara::DSL)
Expand Down
9 changes: 9 additions & 0 deletions spec/rspec/rails/example/system_example_group_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "spec_helper"
module RSpec::Rails
if defined?(SystemExampleGroup)
RSpec.describe SystemExampleGroup do
it_behaves_like "an rspec-rails example group mixin", :system,
'./spec/system/', '.\\spec\\system\\'
end
end
end

0 comments on commit 2fb410b

Please sign in to comment.