Merge pull request #2671 from ifellinaholeonce/master

Add uniqueness tests for Event scope on Vote model with User association
This commit is contained in:
Stella Rouzi 2020-05-21 08:47:26 +03:00 committed by GitHub
commit a89201ff82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View file

@ -4,6 +4,8 @@ class Vote < ApplicationRecord
belongs_to :user belongs_to :user
belongs_to :event belongs_to :event
validates :user_id, uniqueness: { scope: :event_id }
has_paper_trail ignore: [:updated_at], meta: { conference_id: :conference_id } has_paper_trail ignore: [:updated_at], meta: { conference_id: :conference_id }
delegate :name, to: :user delegate :name, to: :user

23
spec/models/vote_spec.rb Normal file
View file

@ -0,0 +1,23 @@
# frozen_string_literal: true
require 'spec_helper'
describe Vote do
let!(:vote) { create(:vote) }
describe 'validation' do
it 'has a valid factory' do
expect(build(:vote)).to be_valid
end
it { is_expected.to validate_uniqueness_of(:user_id).scoped_to(:event_id) }
# This is testing the relationship instead of using the shoulda-matchers
context 'vote with user already exists' do
it 'fails when adding vote twice for user and event' do
expect { create(:vote, user: vote.user, event: vote.event) }
.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: User has already been taken')
end
end
end
end