Commit d52237a6 authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Improve uniqueness validation error message

parent 3fdccc12
......@@ -2,6 +2,7 @@ class RelatedIssue < ActiveRecord::Base
belongs_to :issue
belongs_to :related_issue, class_name: 'Issue'
validates :issue, presence: true, uniqueness: { scope: :related_issue_id }
validates :issue, presence: true
validates :related_issue, presence: true
validates :issue, uniqueness: { scope: :related_issue_id, message: 'is already related' }
end
......@@ -11,6 +11,20 @@ describe RelatedIssue do
it { is_expected.to validate_presence_of(:issue) }
it { is_expected.to validate_presence_of(:related_issue) }
it { is_expected.to validate_uniqueness_of(:issue).scoped_to(:related_issue_id) }
it do
is_expected.to validate_uniqueness_of(:issue)
.scoped_to(:related_issue_id)
.with_message(/already related/)
end
context 'self relation' do
it 'invalidates object' do
issue = create :issue
related_issue = build :related_issue, issue: issue, related_issue: issue
expect(related_issue).to be_invalid
expect(related_issue.errors[:issue]).to include("cannot be related to itself")
end
end
end
end
......@@ -136,7 +136,7 @@ describe RelatedIssues::CreateService, service: true do
end
it 'returns error' do
is_expected.to eq(message: "Validation failed: Issue has already been taken", status: :error, http_status: 401)
is_expected.to eq(message: "Validation failed: Issue is already related", status: :error, http_status: 401)
end
it 'no relation is created' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment