mirror of
https://github.com/astuto/astuto.git
synced 2025-12-15 11:17:49 +01:00
Add comment model and its tests
This commit is contained in:
8
spec/factories/comments.rb
Normal file
8
spec/factories/comments.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
FactoryBot.define do
|
||||
factory :comment do
|
||||
body { "MyText" }
|
||||
user
|
||||
post
|
||||
parent { nil }
|
||||
end
|
||||
end
|
||||
50
spec/models/comment_spec.rb
Normal file
50
spec/models/comment_spec.rb
Normal file
@@ -0,0 +1,50 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Comment, type: :model do
|
||||
let(:comment) { FactoryBot.build(:comment) }
|
||||
|
||||
it 'should be valid' do
|
||||
expect(comment).to be_valid
|
||||
end
|
||||
|
||||
it 'has a body' do
|
||||
nil_body = FactoryBot.build(:comment, body: nil)
|
||||
empty_body = FactoryBot.build(:comment, body: '')
|
||||
|
||||
expect(nil_body).to be_invalid
|
||||
expect(empty_body).to be_invalid
|
||||
end
|
||||
|
||||
it 'has a body with 4 or more characters' do
|
||||
short_body = FactoryBot.build(:comment, body: 'a' * 3)
|
||||
|
||||
expect(short_body).to be_invalid
|
||||
end
|
||||
|
||||
it 'can have no parent' do
|
||||
no_parent = FactoryBot.build(:comment, parent: nil)
|
||||
|
||||
expect(no_parent).to be_valid
|
||||
expect(no_parent.parent).to be_nil
|
||||
end
|
||||
|
||||
it 'can have a parent' do
|
||||
parent = FactoryBot.build(:comment)
|
||||
child = FactoryBot.build(:comment, parent: parent)
|
||||
|
||||
expect(child).to be_valid
|
||||
expect(child.parent).to eq(parent)
|
||||
end
|
||||
|
||||
it 'can have no children' do
|
||||
expect(comment.children).to be_empty
|
||||
end
|
||||
|
||||
it 'can have 1+ children' do
|
||||
parent = FactoryBot.create(:comment)
|
||||
child1 = FactoryBot.create(:comment, parent: parent)
|
||||
child2 = FactoryBot.create(:comment, parent: parent)
|
||||
|
||||
expect(parent.children.length).to eq(2)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user