From 3d27dacf76a1476ac1719725b708e0473f5e7b87 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Sat, 14 Sep 2024 16:34:29 -0400 Subject: [PATCH] Add coverage for `AccountPin` validations --- app/models/account_pin.rb | 2 +- spec/models/account_pin_spec.rb | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 spec/models/account_pin_spec.rb diff --git a/app/models/account_pin.rb b/app/models/account_pin.rb index 6c78e8c4465..3db7fff2045 100644 --- a/app/models/account_pin.rb +++ b/app/models/account_pin.rb @@ -18,7 +18,7 @@ class AccountPin < ApplicationRecord belongs_to :account belongs_to :target_account, class_name: 'Account' - validate :validate_follow_relationship + validate :validate_follow_relationship, if: -> { account.present? } private diff --git a/spec/models/account_pin_spec.rb b/spec/models/account_pin_spec.rb new file mode 100644 index 00000000000..bd8bcbf26a0 --- /dev/null +++ b/spec/models/account_pin_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe AccountPin do + describe 'Associations' do + it { is_expected.to belong_to(:account).required } + it { is_expected.to belong_to(:target_account).required } + end + + describe 'Validations' do + describe 'the follow relationship' do + subject { described_class.new account: account } + + let(:account) { Fabricate :account } + let(:target_account) { Fabricate :account } + + context 'when account is following target account' do + before { account.follow!(target_account) } + + it { is_expected.to allow_value(target_account).for(:target_account).against(:base) } + end + + context 'when account is not following target account' do + it { is_expected.to_not allow_value(target_account).for(:target_account).against(:base).with_message(not_following_message) } + + def not_following_message + I18n.t('accounts.pin_errors.following') + end + end + end + end +end