-
Notifications
You must be signed in to change notification settings - Fork 434
/
comment_spec.rb
111 lines (91 loc) · 3.95 KB
/
comment_spec.rb
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
require "rails_helper"
RSpec.describe Comment do
let(:comment_package) { create(:comment_package) }
let(:comment_package_with_parent) { create(:comment_package, parent: comment_package, commentable: comment_package.commentable) }
let(:comment_package_with_parent_2) { create(:comment_package, parent: comment_package, commentable: comment_package.commentable) }
let(:comment_package_with_grandparent) { create(:comment_package, parent: comment_package_with_parent, commentable: comment_package.commentable) }
describe "has a valid Factory" do
it { expect(comment_package).to be_valid }
end
describe "associations" do
it { is_expected.to belong_to(:commentable) }
it { is_expected.to belong_to(:user).inverse_of(:comments) }
it { is_expected.to have_many(:children).dependent(:destroy).class_name('Comment').with_foreign_key('parent_id') }
end
describe "validations" do
it { is_expected.to validate_presence_of(:body) }
it { is_expected.to validate_presence_of(:commentable) }
it { is_expected.to validate_presence_of(:user) }
it {
expect { create(:comment_package, parent: comment_package) }.to raise_error(
ActiveRecord::RecordInvalid, "Validation failed: Parent belongs to different object")
}
end
describe "to_xml" do
let(:builder) { Nokogiri::XML::Builder.new }
let(:comment_element) { builder.doc.css('comment') }
context "without parent" do
before do
comment_package.to_xml(builder)
end
it "creates xml with correct attributes and content" do
expect(comment_element.attribute('id').value).to eq(comment_package.id.to_s)
expect(comment_element.attribute('when').value.to_datetime).to eq(comment_package.created_at)
expect(comment_element.attribute('who').value).to eq(comment_package.user.login)
expect(comment_element.text).to match(/^#<Nokogiri::XML::Builder::NodeBuilder:0x\h+>$/)
end
end
context "with parent" do
before do
comment_package_with_parent.to_xml(builder)
end
it "creates xml with correct attributes and content" do
expect(comment_element.attribute('id').value).to eq(comment_package_with_parent.id.to_s)
expect(comment_element.attribute('when').value.to_datetime).to eq(comment_package_with_parent.created_at)
expect(comment_element.attribute('who').value).to eq(comment_package_with_parent.user.login)
expect(comment_element.attribute('parent').value).to eq(comment_package_with_parent.parent_id.to_s)
expect(comment_element.text).to match(/^#<Nokogiri::XML::Builder::NodeBuilder:0x\h+>$/)
end
end
end
describe "blank_or_destroy" do
context "without children" do
before do
comment_package
end
it 'should be destroyed' do
expect { comment_package.blank_or_destroy }.to change { Comment.count }.by(-1)
end
end
context "with nobody parent and a brother" do
before do
comment_package_with_parent
comment_package_with_parent_2
comment_package.blank_or_destroy
end
it 'should be destroyed' do
expect { comment_package_with_parent.blank_or_destroy }.to change { Comment.count }.by(-1)
end
end
context "with nobody parent, nobody grandparent and no brother" do
before do
comment_package_with_grandparent
comment_package_with_parent.blank_or_destroy
comment_package.blank_or_destroy
end
it 'should be destroyed' do
expect { comment_package_with_grandparent.blank_or_destroy }.to change { Comment.count }.by(-3)
end
end
context "with children" do
before do
comment_package_with_parent
end
it "shouldn't be destroyed" do
expect { comment_package.blank_or_destroy }.to_not change { Comment.count }
expect(comment_package.body).to eq 'This comment has been deleted'
expect(comment_package.user.login).to eq '_nobody_'
end
end
end
end