Improved RSpec system_init_done helper for DB migrations

Since DB migrations usually always check for system_init_done this is the new default behavior for all examples in a `type: :db_migration` spec. The default state is that Zammad is already initialized. This can be changed by passing `false` as a value to the meta tag `system_init_done`. See `spec/support/db_migration.rb` for more information.
This commit is contained in:
Thorsten Eckel 2018-03-28 11:46:39 +02:00
parent 2eaae8e5c4
commit 1e04b35d5a
3 changed files with 28 additions and 19 deletions

View file

@ -2,9 +2,7 @@ require 'rails_helper'
RSpec.describe CheckForObjectAttributes, type: :db_migration do
it 'performs no action for new systems' do
system_init_done(false)
it 'performs no action for new systems', system_init_done: false do
migrate do |instance|
expect(instance).not_to receive(:attributes)
end
@ -13,8 +11,6 @@ RSpec.describe CheckForObjectAttributes, type: :db_migration do
context 'valid [:data_option]' do
it 'does not change converted text attribute' do
system_init_done
attribute = create(:object_manager_attribute_text)
expect do
@ -25,8 +21,6 @@ RSpec.describe CheckForObjectAttributes, type: :db_migration do
end
it 'does not change select attribute' do
system_init_done
attribute = create(:object_manager_attribute_select)
expect do
@ -37,8 +31,6 @@ RSpec.describe CheckForObjectAttributes, type: :db_migration do
end
it 'does not change tree_select attribute' do
system_init_done
attribute = create(:object_manager_attribute_tree_select)
expect do
@ -52,8 +44,6 @@ RSpec.describe CheckForObjectAttributes, type: :db_migration do
context '[:data_option]' do
it 'ensures an empty Hash' do
system_init_done
attribute = create(:object_manager_attribute_text, data_option: nil)
migrate
attribute.reload
@ -65,8 +55,6 @@ RSpec.describe CheckForObjectAttributes, type: :db_migration do
context '[:data_option][:options]' do
it 'ensures an empty Hash' do
system_init_done
attribute = create(:object_manager_attribute_text, data_option: {})
migrate
attribute.reload
@ -75,8 +63,6 @@ RSpec.describe CheckForObjectAttributes, type: :db_migration do
end
it 'converts String to Hash' do
system_init_done
wrong = {
default: '',
options: '',
@ -96,8 +82,6 @@ RSpec.describe CheckForObjectAttributes, type: :db_migration do
context '[:data_option][:relation]' do
it 'ensures an empty String' do
system_init_done
wrong = {
default: '',
options: {},
@ -112,8 +96,6 @@ RSpec.describe CheckForObjectAttributes, type: :db_migration do
end
it 'converts Hash to String' do
system_init_done
wrong = {
default: '',
options: {},

View file

@ -27,6 +27,27 @@ module DbMigrationHelper
instance.migrate(direction)
end
end
def self.included(base)
# Execute in RSpec class context
base.class_exec do
# This method simulates a system that is is already initialized
# aka `Setting.exists?(name: 'system_init_done')`
# It's possible to simulate a not yet initialized system by adding the
# meta tag `system_init_done` to `false` to the needing example:
#
# @example
# it 'does stuff in an unitialized system', system_init_done: false do
#
before(:each) do |example|
initialized = example.metadata.fetch(:system_init_done, true)
system_init_done(initialized)
end
end
end
end
RSpec.configure do |config|

View file

@ -1,5 +1,11 @@
module SystemInitDoneHelper
def system_init_done(state = true)
# generally allow all calls to Setting.find_by to avoid
# RSpec errors where a different Setting is accessed
allow(Setting).to receive(:find_by).and_call_original
# just mock the Setting check for `system_init_done`
# and return the given parameter value
expect(Setting).to receive(:find_by).with(name: 'system_init_done').and_return(state)
end
end