Browse Source

Try database cleaner approach

pull/58/head
baarkerlounger 4 years ago
parent
commit
6e7d537230
  1. 18
      Gemfile
  2. 5
      Gemfile.lock
  3. 2
      spec/factories/case_log.rb
  4. 44
      spec/rails_helper.rb

18
Gemfile

@ -31,15 +31,8 @@ gem "devise"
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem "byebug", platforms: %i[mri mingw x64_mingw]
gem "capybara", require: false
gem "dotenv-rails"
gem "factory_bot_rails"
gem "pry-byebug"
gem "selenium-webdriver", require: false
gem "simplecov", require: false
%w[rspec-core rspec-expectations rspec-mocks rspec-rails rspec-support].each do |lib|
gem lib, git: "https://github.com/rspec/#{lib}.git", branch: "main", require: false
end
end
group :development do
@ -56,5 +49,16 @@ group :development do
gem "scss_lint-govuk"
end
group :test do
gem "capybara", require: false
gem "factory_bot_rails"
gem "selenium-webdriver", require: false
gem "simplecov", require: false
gem "database_cleaner-active_record", require: false
%w[rspec-core rspec-expectations rspec-mocks rspec-rails rspec-support].each do |lib|
gem lib, git: "https://github.com/rspec/#{lib}.git", branch: "main", require: false
end
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[mingw mswin x64_mingw jruby]

5
Gemfile.lock

@ -140,6 +140,10 @@ GEM
coderay (1.1.3)
concurrent-ruby (1.1.9)
crass (1.0.6)
database_cleaner-active_record (2.0.1)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0.0)
database_cleaner-core (2.0.1)
deep_merge (1.2.1)
devise (4.8.0)
bcrypt (~> 3.0)
@ -390,6 +394,7 @@ DEPENDENCIES
bootsnap (>= 1.4.4)
byebug
capybara
database_cleaner-active_record
devise
discard
dotenv-rails

2
spec/factories/case_log.rb

@ -1,6 +1,6 @@
FactoryBot.define do
factory :case_log do
id { (CaseLog.maximum(:id) || 0) + 1 }
sequence(:id) { |i| i }
trait :in_progress do
status { 1 }
tenant_code { "TH356" }

44
spec/rails_helper.rb

@ -6,6 +6,7 @@ require File.expand_path("../config/environment", __dir__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require "rspec/rails"
require "capybara/rspec"
require 'database_cleaner/active_record'
# Comment to run `js: true specs` with visible browser interaction
Capybara.javascript_driver = :selenium_headless
@ -42,7 +43,48 @@ RSpec.configure do |config|
# 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 = true
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
unless 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
# You can uncomment this line to turn off ActiveRecord support entirely.
# config.use_active_record = false

Loading…
Cancel
Save