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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Add factory methods to ServiceResult
Christophe Bliard76ccb78e0f83
diff --git a/app/services/service_result.rb b/app/services/service_result.rbindex 35ae24105e8..eac8a70be2f 100644--- a/app/services/service_result.rb+++ b/app/services/service_result.rb@@ -33,6 +33,38 @@ class ServiceResult:state,:dependent_results+ # Creates a successful ServiceResult.+ def self.success(errors: nil,+ message: nil,+ message_type: nil,+ state: ::Shared::ServiceState.new,+ dependent_results: [],+ result: nil)+ new(success: true,+ errors:,+ message:,+ message_type:,+ state:,+ dependent_results:,+ result:)+ end++ # Creates a failed ServiceResult.+ def self.failure(errors: nil,+ message: nil,+ message_type: nil,+ state: ::Shared::ServiceState.new,+ dependent_results: [],+ result: nil)+ new(success: false,+ errors:,+ message:,+ message_type:,+ state:,+ dependent_results:,+ result:)+ end+def initialize(success: false,errors: nil,message: nil,diff --git a/spec/services/service_result_spec.rb b/spec/services/service_result_spec.rbindex b76b8fee423..63fc797fcce 100644--- a/spec/services/service_result_spec.rb+++ b/spec/services/service_result_spec.rb@@ -29,36 +29,98 @@require 'spec_helper'describe ServiceResult, type: :model do- let(:instance) { ServiceResult.new }+ let(:instance) { described_class.new }describe 'success' doit 'is what the service is initialized with' do- instance = ServiceResult.new success: true+ instance = described_class.new success: trueexpect(instance.success).to be_truthy- expect(instance.success?).to be_truthy+ expect(instance.success?).to be(true)- instance = ServiceResult.new success: false+ instance = described_class.new success: falseexpect(instance.success).to be_falsey- expect(instance.success?).to be_falsey+ expect(instance.success?).to be(false)endit 'returns what is provided' doinstance.success = trueexpect(instance.success).to be_truthy- expect(instance.success?).to be_truthy+ expect(instance.success?).to be(true)instance.success = falseexpect(instance.success).to be_falsey- expect(instance.success?).to be_falsey+ expect(instance.success?).to be(false)endit 'is false by default' doexpect(instance.success).to be_falsey- expect(instance.success?).to be_falsey+ expect(instance.success?).to be(false)+ end+ end++ describe '.success' do+ it 'creates a ServiceResult with success: true' do+ instance = described_class.success+ expect(instance.success).to be_truthy+ end++ it 'accepts the same set of parameters as the initializer' do+ errors = ['errors']+ message = 'message'+ message_type = :message_type+ state = instance_double(::Shared::ServiceState)+ dependent_results = ['dependent_results']+ result = instance_double(Object, 'result')++ instance = described_class.success(+ errors:,+ message:,+ message_type:,+ state:,+ dependent_results:,+ result:+ )++ expect(instance.errors).to be(errors)+ expect(instance.message).to be(message)+ expect(instance.state).to be(state)+ expect(instance.dependent_results).to be(dependent_results)+ expect(instance.result).to be(result)+ end+ end++ describe '.failure' do+ it 'creates a ServiceResult with success: false' do+ instance = described_class.failure+ expect(instance.success).to be_falsy+ end++ it 'accepts the same set of parameters as the initializer' do+ errors = ['errors']+ message = 'message'+ message_type = :message_type+ state = instance_double(::Shared::ServiceState)+ dependent_results = ['dependent_results']+ result = instance_double(Object, 'result')++ instance = described_class.failure(+ errors:,+ message:,+ message_type:,+ state:,+ dependent_results:,+ result:+ )++ expect(instance.errors).to be(errors)+ expect(instance.message).to be(message)+ expect(instance.state).to be(state)+ expect(instance.dependent_results).to be(dependent_results)+ expect(instance.result).to be(result)endend@@ -72,7 +134,7 @@ describe ServiceResult, type: :model doendit 'is what the object is initialized with' do- instance = ServiceResult.new errors: errors+ instance = described_class.new errors: errorsexpect(instance.errors).to eql errorsend@@ -81,21 +143,21 @@ describe ServiceResult, type: :model doexpect(instance.errors).to be_a ActiveModel::Errorsend- context 'providing errors from user' do+ context 'when providing errors from user' dolet(:result) { build :work_package }it 'creates a new errors instance' do- instance = ServiceResult.new result: result+ instance = described_class.new result: resultexpect(instance.errors).not_to eq result.errorsendendenddescribe 'result' do- let(:result) { double('result') }+ let(:result) { instance_double(Object, 'result') }it 'is what the object is initialized with' do- instance = ServiceResult.new result: result+ instance = described_class.new result: resultexpect(instance.result).to eql resultend@@ -107,15 +169,13 @@ describe ServiceResult, type: :model doendit 'is nil by default' do- instance = ServiceResult.new+ instance = described_class.newexpect(instance.result).to be_nilendenddescribe 'apply_flash_message!' do- let(:service_result) { described_class.new(message:, **params) }- let(:params) { {} }let(:message) { 'some message' }subject(:flash) do@@ -123,19 +183,19 @@ describe ServiceResult, type: :model doendcontext 'when successful' do- let(:params) { { success: true } }+ let(:service_result) { described_class.success(message:) }it { is_expected.to include(notice: message) }endcontext 'when failure' do- let(:params) { { success: false } }+ let(:service_result) { described_class.failure(message:) }it { is_expected.to include(error: message) }endcontext 'when setting message_type to :info' do- let(:params) { { message_type: :info } }+ let(:service_result) { described_class.success(message:, message_type: :info) }it { is_expected.to include(info: message) }end