From a3550a55a5053246f948cd884cb3f283fa797f3e Mon Sep 17 00:00:00 2001 From: David Gil Date: Wed, 12 Aug 2015 14:57:18 +0200 Subject: [PATCH] create spec/models/comment_spec and add a few tests for children count --- spec/models/comment_spec.rb | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 spec/models/comment_spec.rb diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb new file mode 100644 index 000000000..5af7afc1c --- /dev/null +++ b/spec/models/comment_spec.rb @@ -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