From ae07192ecc7b2cfd4efd0877af2ac59d1ba7e377 Mon Sep 17 00:00:00 2001 From: Daniel M Brasil Date: Thu, 22 Feb 2024 14:29:10 -0300 Subject: [PATCH] improve mention model test coverage (#20652) --- spec/models/mention_spec.rb | 54 +++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/spec/models/mention_spec.rb b/spec/models/mention_spec.rb index dc9c15866..8a0b1a1ee 100644 --- a/spec/models/mention_spec.rb +++ b/spec/models/mention_spec.rb @@ -1,9 +1,9 @@ require "rails_helper" RSpec.describe Mention do - let(:comment) { create(:comment, commentable: create(:podcast_episode)) } + describe ".create_all" do + let(:comment) { create(:comment, commentable: create(:podcast_episode)) } - describe "#create_all" do it "enqueues a job to default queue" do expect do described_class.create_all(comment) @@ -11,13 +11,51 @@ RSpec.describe Mention do end end - # TODO: Replace this test with validation spec - it "creates a valid mention" do - expect(create(:mention)).to be_valid + describe "validations" do + it { is_expected.to validate_presence_of(:mentionable_type) } + + context "when validating uniqueness of user_id" do + let(:user) { create(:user) } + let(:mention) { create(:mention, user: user) } + + it "is scoped to mentionable_id and mentionable_type" do + duplicate_mention = build(:mention, user: user, mentionable: mention.mentionable) + + expect(duplicate_mention).not_to be_valid + end + + it "allows the same user_id for different mentionable_id and mentionable_type" do + other_mention = build(:mention, user: user) + + expect(other_mention).to be_valid + end + end + + context "when mentionable is invalid" do + let(:comment) { build(:comment, commentable: nil) } + + it "is invalid" do + mention = build(:mention, mentionable: comment) + + expect(mention).not_to be_valid + end + end end - # TODO: Replace this test with validation spec - it "doesn't raise undefined method for NilClass on valid?" do - expect(described_class.new.valid?).to be(false) + describe "associations" do + it { is_expected.to belong_to(:user) } + it { is_expected.to belong_to(:mentionable) } + end + + describe "callbacks" do + let(:mention) { build(:mention) } + + it "enqueues SendEmailNotificationWorker after create" do + allow(Mentions::SendEmailNotificationWorker).to receive(:perform_async) + + mention.save + + expect(Mentions::SendEmailNotificationWorker).to have_received(:perform_async).with(mention.id) + end end end