Skip to content

Commit

Permalink
Merge pull request #13 from VladyslavKutsevolov/feature/setup-feature…
Browse files Browse the repository at this point in the history
…-specs

Capybara and Poltergeist Setup
  • Loading branch information
VladyslavKutsevolov authored Oct 20, 2020
2 parents 4e564d9 f332518 commit 3e48907
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 47,12 @@ group :development, :test do
gem 'rspec-rails', '~> 3.5'
end

group :test do
gem 'capybara'
gem 'poltergeist'
gem 'database_cleaner'
end

group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
Expand Down
25 changes: 25 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 36,8 @@ GEM
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
arel (6.0.3)
autoprefixer-rails (6.3.7)
execjs
Expand All @@ -47,12 49,21 @@ GEM
sass (>= 3.3.4)
builder (3.2.2)
byebug (9.0.5)
capybara (2.18.0)
addressable
mini_mime (>= 0.1.3)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (>= 2.0, < 4.0)
carrierwave (0.10.0)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
json (>= 1.7)
mime-types (>= 1.16)
cliver (0.3.2)
concurrent-ruby (1.0.2)
database_cleaner (1.8.5)
debug_inspector (0.0.2)
diff-lcs (1.4.4)
domain_name (0.5.20160615)
Expand Down Expand Up @@ -86,6 97,7 @@ GEM
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_mime (1.0.2)
mini_portile2 (2.1.0)
minitest (5.9.0)
monetize (1.1.0)
Expand All @@ -105,6 117,11 @@ GEM
pkg-config (~> 1.1.7)
pg (0.18.4)
pkg-config (1.1.7)
poltergeist (1.18.1)
capybara (>= 2.1, < 4)
cliver (~> 0.3.1)
websocket-driver (>= 0.2.0)
public_suffix (4.0.6)
puma (3.5.2)
quiet_assets (1.0.3)
railties (>= 3.1, < 5.0)
Expand Down Expand Up @@ -203,6 220,11 @@ GEM
binding_of_caller (>= 0.7.2)
railties (>= 4.0)
sprockets-rails (>= 2.0, < 4.0)
websocket-driver (0.7.3)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
xpath (2.1.0)
nokogiri (~> 1.3)

PLATFORMS
ruby
Expand All @@ -211,7 233,9 @@ DEPENDENCIES
bcrypt (~> 3.1.7)
bootstrap-sass (~> 3.3.6)
byebug
capybara
carrierwave
database_cleaner
dotenv-rails
faker
font-awesome-rails
Expand All @@ -220,6 244,7 @@ DEPENDENCIES
money-rails
newrelic_rpm
pg
poltergeist
puma
quiet_assets
rails (= 4.2.6)
Expand Down
11 changes: 9 additions & 2 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 1,12 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
require "capybara/rails"
require "capybara/rspec"
require "capybara/poltergeist" # Add this line to require poltergeist

# Specs flagged with `js: true` will use Capybara's JS driver. Set
# that JS driver to :poltergeist
Capybara.javascript_driver = :poltergeist
ENV['RAILS_ENV'] ||= 'test'

require File.expand_path('../config/environment', __dir__)
Expand All @@ -22,7 29,7 @@
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
Expand All @@ -39,7 46,7 @@
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
config.use_transactional_fixtures = false

# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
Expand Down
44 changes: 44 additions & 0 deletions spec/support/database_cleaner.rb
Original file line number Diff line number Diff line change
@@ -0,0 1,44 @@
RSpec.configure do |config|

config.before(:suite) do
if config.use_transactional_fixtures?
raise(<<-MSG)
Delete line `config.use_transactional_fixtures = true` from rails_helper.rb
(or set it to false) to prevent uncommitted transactions being used in
JavaScript-dependent specs.
During testing, the app-under-test that the browser driver connects to
uses a different database connection to the database connection used by
the spec. The app's database connection would not be able to access
uncommitted transaction data setup over the spec's database connection.
MSG
end
DatabaseCleaner.clean_with(:truncation)
end

config.before(:each) do
DatabaseCleaner.strategy = :transaction
end

config.before(:each, type: :feature) do
# :rack_test driver's Rack app under test shares database connection
# with the specs, so continue to use transaction strategy for speed.
driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test

if !driver_shares_db_connection_with_specs
# Driver is probably for an external browser with an app
# under test that does *not* share a database connection with the
# specs, so use truncation strategy.
DatabaseCleaner.strategy = :truncation
end
end

config.before(:each) do
DatabaseCleaner.start
end

config.append_after(:each) do
DatabaseCleaner.clean
end

end

0 comments on commit 3e48907

Please sign in to comment.