Refactoring: Move all RSpec configurations to dedicated support files.

This commit is contained in:
Thorsten Eckel 2021-07-22 18:52:09 +02:00
parent 9c7ddb7a9d
commit dc18e883ce
7 changed files with 45 additions and 56 deletions

View file

@ -6,8 +6,10 @@ require File.expand_path('../config/environment', __dir__)
# Prevent database truncation if the environment is production # Prevent database truncation if the environment is production
abort('The Rails environment is running in production mode!') if Rails.env.production? abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'spec_helper' require 'spec_helper'
require 'rspec/rails' require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point! # Add additional requires below this line. Rails is not loaded until this point!
# Requires supporting ruby files with custom matchers and macros, etc, in # Requires supporting ruby files with custom matchers and macros, etc, in
@ -24,58 +26,3 @@ require 'rspec/rails'
# require only the support files necessary. # require only the support files necessary.
# #
Dir[Rails.root.join('spec/support/**/*.rb')].sort.each { |f| require f } Dir[Rails.root.join('spec/support/**/*.rb')].sort.each { |f| require f }
# Reset database to be ready for tests
Rake::Task['zammad:db:reset'].execute
# make sure that all migrations of linked packages are executed
Package::Migration.linked
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# make usage of time travel helpers possible
config.include ActiveSupport::Testing::TimeHelpers
config.after do
travel_back
end
# Zammad specific helpers
config.include ZammadHelper
# skip Zammad helper functions in the stacktrace to lower noise
config.backtrace_exclusion_patterns << %r{/spec/spec_helper/}
# 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
# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, :type => :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
# config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end

View file

@ -1,4 +1,9 @@
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
require_relative './rake'
# Reset database to be ready for tests
Rake::Task['zammad:db:reset'].execute
# make sure that all migrations of linked packages are executed # make sure that all migrations of linked packages are executed
Package::Migration.linked Package::Migration.linked

View file

@ -0,0 +1,8 @@
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
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
end

5
spec/support/fixtures.rb Normal file
View file

@ -0,0 +1,5 @@
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
end

View file

@ -1,7 +1,15 @@
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
module ZammadHelper module OtrsImportJsonFixtureHelper
def json_fixture(file) def json_fixture(file)
JSON.parse(File.read("spec/fixtures/#{file}.json")) JSON.parse(File.read("spec/fixtures/#{file}.json"))
end end
end end
RSpec.configure do |config|
# Zammad specific helpers
config.include OtrsImportJsonFixtureHelper
# skip OtrsImportJsonFixtureHelper functions in the backtraces to lower noise
config.backtrace_exclusion_patterns << %r{/spec/spec_helper/}
end

View file

@ -0,0 +1,8 @@
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
RSpec.configure do |config| # rubocop:disable Style/SymbolProc
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end

View file

@ -0,0 +1,8 @@
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end