create spec/models/comment_spec and add a few tests for children count
This commit is contained in:
41
spec/models/comment_spec.rb
Normal file
41
spec/models/comment_spec.rb
Normal 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
|
||||
Reference in New Issue
Block a user