docbrown/spec/models/notification_subscription_spec.rb
Jeremy Friesen b115b2d17e
Appeasing Rubocop as it sneaks some changes in (#16085)
I was working on another branch and as part of my commit, Rubocop
removed a validation (but not the spec that asserted the validation).

Below is the "non-updating" rubocop offense on the other branch.

```shell
❯ rubocop ./app/models/notification_subscription.rb
Inspecting 1 file
C

Offenses:

app/models/notification_subscription.rb:13:29: C: [Correctable]
Rails/RedundantPresenceValidationOnBelongsTo: Remove explicit presence
validation for notifiable_id.
  validates :notifiable_id, presence: true
                            ^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense auto-correctable
```

To remediate, I ran:

```shell
> rubocop --only "Rails/RedundantPresenceValidationOnBelongsTo" \
  --auto-correct
```

This resolved the `app/models`.  Then did some regex magic and removed
the assertions from `spec/models`.

For Forem folks, I wrote a [forem.team post][1] discuss if this is how
we want to proceed.

[1]:https://forem.team/jeremy/rubocop-auto-updating-mayhem-33a6
2022-01-13 07:48:01 -05:00

51 lines
1.6 KiB
Ruby

require "rails_helper"
RSpec.describe NotificationSubscription, type: :model do
let(:user) { create(:user) }
let(:article) { create(:article, user: user) }
let(:notification_subscription) { create(:notification_subscription, user: user, notifiable: article) }
describe "validations" do
describe "builtin validations" do
subject(:subscription) { notification_subscription }
it { is_expected.to belong_to(:notifiable) }
it { is_expected.to belong_to(:user) }
it do
expect(subscription).to(
validate_inclusion_of(:config).in_array(%w[all_comments top_level_comments only_author_comments]),
)
end
it { is_expected.to validate_presence_of(:config) }
it { is_expected.to validate_presence_of(:notifiable_type) }
it { is_expected.to validate_uniqueness_of(:user_id).scoped_to(%i[notifiable_type notifiable_id]) }
end
describe "#notifiable_type" do
it "is valid if equals to Article" do
notification_subscription.notifiable_type = "Article"
expect(notification_subscription).to be_valid
end
it "is valid if equals to Comment" do
comment = create(:comment)
notification_subscription.notifiable_id = comment.id
notification_subscription.notifiable_type = "Comment"
expect(notification_subscription).to be_valid
end
it "is is invalid with Podcast" do
podcast = create(:podcast)
notification_subscription.notifiable_id = podcast.id
notification_subscription.notifiable_type = "Podcast"
expect(notification_subscription).not_to be_valid
end
end
end
end