-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathcomment_spec.rb
More file actions
64 lines (54 loc) · 2.17 KB
/
comment_spec.rb
File metadata and controls
64 lines (54 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# spec/models/comment_spec.rb
require 'rails_helper'
RSpec.describe Comment, type: :model do
describe 'associations' do
it { should belong_to(:commentable) }
end
describe 'validations' do
it 'is valid with valid attributes' do
user = create(:user)
comment = build(:comment, commentable: user, body: "Test comment")
expect(comment).to be_valid
end
it 'is invalid without a body' do
user = create(:user)
comment = build(:comment, commentable: user, body: nil)
expect(comment).not_to be_valid
end
end
describe 'polymorphic association' do
let!(:user) { create(:user) }
let!(:person) { create(:person) }
it 'can be associated with a User' do
comment = create(:comment, commentable: user, body: "User comment")
expect(comment.commentable).to eq(user)
expect(user.comments).to include(comment)
end
it 'can be associated with a Person' do
comment = create(:comment, commentable: person, body: "Person comment")
expect(comment.commentable).to eq(person)
expect(person.comments).to include(comment)
end
it 'can be associated with an EventRegistration' do
event_registration = create(:event_registration)
comment = create(:comment, commentable: event_registration, body: "Registration comment")
expect(comment.commentable).to eq(event_registration)
expect(event_registration.comments).to include(comment)
end
it 'can be associated with a Workshop' do
workshop = create(:workshop)
comment = create(:comment, commentable: workshop, body: "Workshop comment")
expect(comment.commentable).to eq(workshop)
expect(workshop.comments).to include(comment)
end
end
describe 'scopes' do
let!(:user) { create(:user) }
let!(:old_comment) { create(:comment, commentable: user, body: "Old comment", created_at: 2.days.ago) }
let!(:new_comment) { create(:comment, commentable: user, body: "New comment", created_at: 1.day.ago) }
it 'orders comments by created_at descending with newest_first scope' do
expect(user.comments.first).to eq(new_comment)
expect(user.comments.last).to eq(old_comment)
end
end
end