create spec/models/comment_spec and add a few tests for children count

This commit is contained in:
David Gil
2015-08-12 14:57:18 +02:00
parent 9a2ef8366e
commit a3550a55a5

View File

@@ -0,0 +1,41 @@
require 'rails_helper'
describe Comment do
let(:comment) { build(:comment) }
it "should be valid" do
expect(comment).to be_valid
end
describe "#children_count" do
let(:comment) { create(:comment) }
let(:debate) { comment.debate }
it "should count first level children" do
parent = comment
3.times do
create(:comment, commentable: debate).
move_to_child_of(parent)
parent = parent.children.first
end
expect(comment.children_count).to eq(1)
expect(debate.comment_threads.count).to eq(4)
end
it "should increase children count" do
expect do
create(:comment, commentable: debate).
move_to_child_of(comment)
end.to change { comment.children_count }.from(0).to(1)
end
it "should decrease children count" do
new_comment = create(:comment, commentable: debate).move_to_child_of(comment)
expect { new_comment.destroy }.to change { comment.children_count }.from(1).to(0)
end
end
end