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

Fix service specs

Oliver Güntherca0629f5cf0f
diff --git a/app/services/service_result.rb b/app/services/service_result.rb
index e7b78e29293..a2154e8f17d 100644
--- a/app/services/service_result.rb
+++ b/app/services/service_result.rb
@@ -180,14 +180,13 @@ class ServiceResult
private
def initialize_errors(errors)
- self.errors =
- if errors
- errors
- elsif result.respond_to?(:errors)
- ActiveModel::Errors.new(self).tap { |e| e.merge! result }
- else
- ActiveModel::Errors.new(self)
- end
+ self.errors = errors || new_errors_with_result
+ end
+
+ def new_errors_with_result
+ ActiveModel::Errors.new(self).tap do |errors|
+ errors.merge!(result) if result.respond_to?(:errors)
+ end
end
def get_message_type
diff --git a/spec/lib/journal_formatter/custom_field_spec.rb b/spec/lib/journal_formatter/custom_field_spec.rb
index ecaf7b20624..3cd3a869842 100644
--- a/spec/lib/journal_formatter/custom_field_spec.rb
+++ b/spec/lib/journal_formatter/custom_field_spec.rb
@@ -70,6 +70,7 @@ describe OpenProject::JournalFormatter::CustomField do
let(:expected) do
I18n.t(:text_journal_changed,
label: "<strong>#{custom_field.name}</strong>",
+ linebreak: '',
old: "<i title=\"#{old_formatted_value}\">#{old_formatted_value}</i>",
new: "<i title=\"#{new_formatted_value}\">#{new_formatted_value}</i>")
end
@@ -111,6 +112,7 @@ describe OpenProject::JournalFormatter::CustomField do
I18n.t(:text_journal_changed_plain,
label: custom_field.name,
old: format_value(values.first, custom_field),
+ linebreak: '',
new: format_value(values.last, custom_field))
end
@@ -152,6 +154,7 @@ describe OpenProject::JournalFormatter::CustomField do
let(:expected) do
I18n.t(:text_journal_changed,
label: "<strong>#{I18n.t(:label_deleted_custom_field)}</strong>",
+ linebreak: '',
old: "<i title=\"#{values.first}\">#{values.first}</i>",
new: "<i title=\"#{values.last}\">#{values.last}</i>")
end
@@ -229,6 +232,7 @@ describe OpenProject::JournalFormatter::CustomField do
let(:expected) do
I18n.t(:text_journal_changed,
label: "<strong>#{custom_field.name}</strong>",
+ linebreak: '',
old: "<i title=\"cf 1, cf 2\">cf 1, cf 2</i>",
new: "<i title=\"cf 3, cf 4\">cf 3, cf 4</i>")
end
@@ -243,6 +247,7 @@ describe OpenProject::JournalFormatter::CustomField do
let(:expected) do
I18n.t(:text_journal_changed,
label: "<strong>#{custom_field.name}</strong>",
+ linebreak: '',
old: "<i title=\"cf 1, cf 2\">cf 1, cf 2</i>",
new: "<i title=\"(deleted option), cf 4\">(deleted option), cf 4</i>")
end
diff --git a/spec/services/base_services/behaves_like_delete_service.rb b/spec/services/base_services/behaves_like_delete_service.rb
index 104d780af59..d33d97071ab 100644
--- a/spec/services/base_services/behaves_like_delete_service.rb
+++ b/spec/services/base_services/behaves_like_delete_service.rb
@@ -60,7 +60,7 @@ shared_examples 'BaseServices delete service' do
let(:model_destroy_result) { true }
let(:contract_validate_result) { true }
- let(:contract_errors) { double(ActiveModel::Errors) }
+ let(:contract_errors) { ActiveModel::Errors.new(instance) }
before do
allow(model_instance).to receive(:destroy).and_return(model_destroy_result)
@@ -99,7 +99,7 @@ shared_examples 'BaseServices delete service' do
context 'when model cannot be destroyed' do
let(:model_destroy_result) { false }
- let(:model_errors) { instance_double(ActiveModel::Errors) }
+ let(:model_errors) { ActiveModel::Errors.new(model_instance) }
it 'is unsuccessful' do
expect(subject)
@@ -107,11 +107,14 @@ shared_examples 'BaseServices delete service' do
end
it "returns the user's errors" do
+ model_errors.add :base, 'This is some error.'
+
allow(model_instance)
.to(receive(:errors))
.and_return model_errors
expect(subject.errors).to eql model_errors
+ expect(subject.errors[:base]).to include "This is some error."
end
end
end
diff --git a/spec/services/work_packages/update_service_spec.rb b/spec/services/work_packages/update_service_spec.rb
index f5f7866b8a8..2e60c47e47d 100644
--- a/spec/services/work_packages/update_service_spec.rb
+++ b/spec/services/work_packages/update_service_spec.rb
@@ -117,13 +117,9 @@ describe WorkPackages::UpdateService, type: :model do
end
context 'when setting the attributes is unsuccessful (invalid)' do
- let(:errors) { double('set errors', empty?: false) }
+ let(:errors) { ActiveModel::Errors.new(work_package) }
let(:set_service_results) { ServiceResult.new success: false, errors: errors, result: work_package }
- before do
- allow(errors).to receive(:merge!)
- end
-
it 'is unsuccessful' do
expect(subject.success?).to be_falsey
end
@@ -135,18 +131,20 @@ describe WorkPackages::UpdateService, type: :model do
end
it 'exposes the errors' do
+ errors.add(:base, 'This is a custom error!')
+
subject
expect(subject.errors).to eql errors
+ expect(subject.errors[:base]).to include 'This is a custom error!'
end
end
context 'when the saving is unsuccessful' do
let(:work_package_save_result) { false }
- let(:saving_errors) { double('saving_errors', empty?: false) }
+ let(:saving_errors) { ActiveModel::Errors.new(work_package) }
before do
- allow(saving_errors).to receive(:merge!)
allow(work_package)
.to receive(:errors)
.and_return(saving_errors)
@@ -162,10 +160,13 @@ describe WorkPackages::UpdateService, type: :model do
expect(work_package.changed?).to be_truthy
end
- it "exposes the work_packages's errors" do
+ it 'exposes the errors, but is a new instance' do
+ saving_errors.add(:base, 'This is a custom error!')
+
subject
- expect(subject.errors).to eql saving_errors
+ expect(subject.errors).not_to eql saving_errors
+ expect(subject.errors[:base]).to include 'This is a custom error!'
end
end
end