Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Simplify the test setup

Christophe Bliard8598fc5dc53e
diff --git a/spec/services/settings/update_service_spec.rb b/spec/services/settings/update_service_spec.rb
index 587494b2dd4..f6ef8302047 100644
--- a/spec/services/settings/update_service_spec.rb
+++ b/spec/services/settings/update_service_spec.rb
@@ -27,78 +27,47 @@
require 'spec_helper'
describe Settings::UpdateService do
- let(:user) { build_stubbed(:user) }
- let(:contract_success) { true }
- let(:contract_errors) { instance_double(ActiveModel::Error) }
- let(:contract_options) { {} }
let(:instance) do
described_class.new(user:, contract_options:)
end
- let!(:contract) do
- contract_instance = instance_double(Settings::UpdateContract)
-
- allow(Settings::UpdateContract)
- .to receive(:new)
- .and_return(contract_instance)
-
- allow(contract_instance)
- .to receive(:validate)
- .and_return(contract_success)
- allow(contract_instance)
- .to receive(:errors)
- .and_return(contract_errors)
-
- contract_instance
- end
- let!(:params_contract) do
- contract_instance = instance_double(ParamsContract)
-
- allow(ParamsContract)
- .to receive(:new)
- .and_return(contract_instance)
-
- allow(contract_instance)
- .to receive(:valid?)
- .and_return(params_contract_success)
- allow(contract_instance)
- .to receive(:errors)
- .and_return(params_contract_errors)
-
- contract_instance
- end
- let(:params_contract_success) { true }
- let(:params_contract_errors) { instance_double(ActiveModel::Error) }
- let!(:setting) do
- allow(Setting)
- .to receive(:[]=)
- .with(setting_name, setting_value)
+ let(:user) { build_stubbed(:user) }
+ let(:contract_options) { {} }
+ let(:contract) do
+ instance_double(Settings::UpdateContract,
+ validate: contract_success,
+ errors: instance_double(ActiveModel::Error))
end
- let!(:definition) do
- instance_double(Settings::Definition).tap do |definition_instance|
- allow(Settings::Definition)
- .to receive(:[])
- .and_call_original
-
- allow(Settings::Definition)
- .to receive(:[])
- .with(setting_name)
- .and_return(definition_instance)
-
- allow(definition_instance)
- .to receive(:on_change)
- .and_return(definition_on_change)
- end
+ let(:contract_success) { true }
+ let(:setting_definition) do
+ instance_double(Settings::Definition,
+ on_change: definition_on_change)
end
let(:definition_on_change) do
- instance_double(Proc).tap do |proc|
- allow(proc)
- .to receive(:call)
- end
+ instance_double(Proc,
+ call: nil)
end
- let(:setting_name) { :setting_name }
- let(:setting_value) { 'setting_value' }
+ let(:setting_name) { :a_setting_name }
+ let(:setting_value) { 'a_setting_value' }
let(:params) { { setting_name => setting_value } }
+ before do
+ # stub a setting definition
+ allow(Settings::Definition)
+ .to receive(:[])
+ .and_call_original
+ allow(Settings::Definition)
+ .to receive(:[])
+ .with(setting_name)
+ .and_return(setting_definition)
+ allow(Setting)
+ .to receive(:[]=)
+
+ # stub contract
+ allow(Settings::UpdateContract)
+ .to receive(:new)
+ .and_return(contract)
+ end
+
describe '#call' do
shared_examples_for 'successful call' do
it 'is successful' do
@@ -106,11 +75,12 @@ describe Settings::UpdateService do
.to be_success
end
- it 'sets the value' do
+ it 'sets the setting value' do
instance.call(params)
expect(Setting)
.to have_received(:[]=)
+ .with(setting_name, setting_value)
end
it 'calls the on_change handler' do
@@ -127,41 +97,54 @@ describe Settings::UpdateService do
.not_to be_success
end
- it 'does not call the on_change handler' do
+ it 'does not set the setting value' do
instance.call(params)
- expect(definition_on_change)
- .not_to have_received(:call)
+ expect(Setting)
+ .not_to have_received(:[]=)
end
- it 'does not set the value' do
+ it 'does not call the on_change handler' do
instance.call(params)
- expect(Setting)
- .not_to have_received(:[]=)
+ expect(definition_on_change)
+ .not_to have_received(:call)
end
end
- it_behaves_like 'successful call'
+ include_examples 'successful call'
context 'when the contract is not successfully validated' do
let(:contract_success) { false }
- it_behaves_like 'unsuccessful call'
+ include_examples 'unsuccessful call'
end
- context 'with a provided params_contract that is successfully validated' do
+ context 'with a provided params_contract' do
let(:contract_options) { { params_contract: ParamsContract } }
- let(:params_contract_success) { true }
+ let(:params_contract) do
+ instance_double(ParamsContract,
+ valid?: params_contract_success,
+ errors: instance_double(ActiveModel::Error))
+ end
- it_behaves_like 'successful call'
- end
+ before do
+ allow(ParamsContract)
+ .to receive(:new)
+ .and_return(params_contract)
+ end
- context 'with a provided params_contract that fails validation' do
- let(:contract_options) { { params_contract: ParamsContract } }
- let(:params_contract_success) { false }
+ context 'with a provided params_contract that is successfully validated' do
+ let(:params_contract_success) { true }
+
+ include_examples 'successful call'
+ end
- it_behaves_like 'unsuccessful call'
+ context 'with a provided params_contract that fails validation' do
+ let(:params_contract_success) { false }
+
+ include_examples 'unsuccessful call'
+ end
end
end
end